-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_4.js
51 lines (46 loc) · 1.5 KB
/
day_4.js
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
48
49
50
51
// Write a function which can convert the time input given in 12 hours format to 24 hours format
// The check for 'AM' and 'PM' can be verified using endsWith String method
// An extra 0 would be needed if the hours have single digit
const time1 = '12:10AM';
const time2 = '5:00AM';
const time3 = '12:33PM';
const time4 = '01:59PM';
const time5 = '11:8PM';
const time6 = '10:02PM';
function convertTo24HrsFormat(time) {
// write your solution here
if(time.length === 6)
{
if(time.charAt(2) == ':')
time = time.slice(0,3) + '0' + time.slice(3);
else
time = '0' + time;
}
let ret_time;
// console.log(time.slice(-2));
if(time.slice(-2) === 'AM')
{
if(time.slice(0,2) === '12')
ret_time = '00'+time.slice(2,5);
else
ret_time = time.slice(0,5);
}
else
{
if(time.slice(0,2) === '12')
ret_time = time.slice(0,5);
else
{
let hr = (Number(time.slice(0,2)) + 12).toString();
ret_time = hr + time.slice(2,5);
}
}
// console.log(ret_time);
return ret_time;
}
console.log(`Converted time: ${convertTo24HrsFormat(time1)}`);
console.log(`Converted time: ${convertTo24HrsFormat(time2)}`);
console.log(`Converted time: ${convertTo24HrsFormat(time3)}`);
console.log(`Converted time: ${convertTo24HrsFormat(time4)}`);
console.log(`Converted time: ${convertTo24HrsFormat(time5)}`);
console.log(`Converted time: ${convertTo24HrsFormat(time6)}`);