-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeConversion.js
More file actions
39 lines (25 loc) · 983 Bytes
/
timeConversion.js
File metadata and controls
39 lines (25 loc) · 983 Bytes
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
/*
Given a time in -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.
Example
Return '12:01:00'.
Return '00:01:00'.
Function Description
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format.
timeConversion has the following parameter(s):
string s: a time in hour format
Returns
string: the time in hour format
Input Format
A single string that represents a time in -hour clock format (i.e.: or ).
*/
function timeConversion(s) {
let [hour, minutes, last] = s.split(':')
const seconds = last.substring(0,2)
const isPm = last.substring(2) === 'PM'
if (hour === '12' && !isPm) hour = '00'
if (isPm && hour !== '12') hour = (parseInt(hour) + 12)
console.log(`${hour}:${minutes}:${seconds}`)
return `${hour}:${minutes}:${seconds}`
}