Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
給與一個 12 小時制 AM/PM 格式的時間,將其轉換為軍用(24 小時制)時間。
Example 1:
s = '12:01:00PM' Return '12:01:00'.
Example 2:
s = '12:01:00AM' Return '00:01:00'.
solution:
先了解 AM 與 PM 的時間轉換格式,將 PM 的部分進行分類後,額外增加 12,(’12:00:00′ 轉換成 ’24:00:00’。)
再將 AM 的 ’12:00:00′ 轉換成 ’00:00:00’。
Code 1: BigO(n log n)
'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.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function timeConversion(s) {
// Write your code here
let checkAmPm = s.charAt(8),
militaryTime = s.substring(0, 2)
if (checkAmPm === "P" && militaryTime !== "12") {
militaryTime = String(parseInt(militaryTime, 10) + 12);
} else if (checkAmPm === "A" && militaryTime === "12") {
militaryTime = "00";
}
return militaryTime + s.substring(2, 8);
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = readLine();
const result = timeConversion(s);
ws.write(result + '\n');
ws.end();
}
FlowChart:
Example 1
s = '12:01:00PM' checkAmPm = 'P' '12:01:00PM' => '12:01:00'
Example 2
s = '12:01:00AM' checkAmPm = 'A' '12:01:00AM' => '00:01:00'