Skip to content

Commit b622815

Browse files
authored
chore: Merge pull request #678 from suryapratapsinghsuryavanshi/master
added RailwayTimeConversion method in the conversion category.
2 parents a19541b + 34b783d commit b622815

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Conversions/RailwayTimeConversion.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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
5+
want some changes in hour value.
6+
7+
Input Formate -> 07:05:45PM
8+
Output Fromate -> 19:05:45
9+
10+
Problem & Explanation Source : https://www.mathsisfun.com/time.html
11+
*/
12+
13+
/**
14+
* RailwayTimeConversion method converts normalized time string to Railway time string.
15+
* @param {String} timeString Normalized time string.
16+
* @returns {String} Railway time string.
17+
*/
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+
}
33+
}
34+
35+
module.exports = RailwayTimeConversion

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy