forked from DhanushNehru/Hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeConversion.js
More file actions
47 lines (37 loc) · 1.09 KB
/
TimeConversion.js
File metadata and controls
47 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.trim().split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
function timeConversion(s) {
const period = s.slice(-2); // Extract AM or PM
let hour = parseInt(s.slice(0, 2)); // Extract hour as an integer
const rest = s.slice(2, 8); // Extract the remaining time part (minutes and seconds)
if (period === 'AM') {
if (hour === 12) {
hour = 0; // Midnight case: 12 AM -> 00
}
} else { // PM case
if (hour !== 12) {
hour += 12; // Convert PM hour (except 12 PM)
}
}
const hourStr = hour < 10 ? '0' + hour : hour.toString(); // Ensure two-digit hour format
return hourStr + rest;
}
function main() {
const s = readLine();
const result = timeConversion(s);
console.log(result);
}