|
1 | 1 | /*
|
2 |
| - The time conversion of normalized time to the railway is a simple algorithm |
3 |
| - because we know that if the time is in 'AM' value it means they only want |
4 |
| - some changes on hours and minutes and if the time in 'PM' it means the only |
| 2 | + The time conversion of normalized time to the railway is a simple algorithm |
| 3 | + because we know that if the time is in 'AM' value it means they only want |
| 4 | + some changes on hours and minutes and if the time in 'PM' it means the only |
5 | 5 | want some changes in hour value.
|
6 | 6 |
|
7 | 7 | Input Formate -> 07:05:45PM
|
|
16 | 16 | * @returns {String} Railway time string.
|
17 | 17 | */
|
18 | 18 | const RailwayTimeConversion = (timeString) => {
|
19 |
| - // firstly, check that input is a string or not. |
20 |
| - if (typeof timeString !== 'string') { |
21 |
| - return new TypeError('Argument is not a string.') |
22 |
| - } |
23 |
| - // split the string by ':' character. |
24 |
| - const [hour, minute, scondWithShift] = timeString.split(':') |
25 |
| - // split second and shift value. |
26 |
| - const [second, shift] = [scondWithShift.substr(0, 2), scondWithShift.substr(2)] |
27 |
| - // convert shifted time to not-shift time(Railway time) by using the above explanation. |
28 |
| - if (shift === 'PM') { |
29 |
| - if (parseInt(hour) === 12) { return `${hour}:${minute}:${second}` } else { return `${parseInt(hour) + 12}:${minute}:${second}` } |
30 |
| - } else { |
31 |
| - if (parseInt(hour) === 12) { return `00:${minute}:${second}` } else { return `${hour}:${minute}:${second}` } |
32 |
| - } |
| 19 | + // firstly, check that input is a string or not. |
| 20 | + if (typeof timeString !== 'string') { |
| 21 | + return new TypeError('Argument is not a string.') |
| 22 | + } |
| 23 | + // split the string by ':' character. |
| 24 | + const [hour, minute, scondWithShift] = timeString.split(':') |
| 25 | + // split second and shift value. |
| 26 | + const [second, shift] = [scondWithShift.substr(0, 2), scondWithShift.substr(2)] |
| 27 | + // convert shifted time to not-shift time(Railway time) by using the above explanation. |
| 28 | + if (shift === 'PM') { |
| 29 | + if (parseInt(hour) === 12) { return `${hour}:${minute}:${second}` } else { return `${parseInt(hour) + 12}:${minute}:${second}` } |
| 30 | + } else { |
| 31 | + if (parseInt(hour) === 12) { return `00:${minute}:${second}` } else { return `${hour}:${minute}:${second}` } |
| 32 | + } |
33 | 33 | }
|
34 | 34 |
|
35 | 35 | module.exports = RailwayTimeConversion
|
0 commit comments