diff --git a/src/timeConversion.js b/src/timeConversion.js new file mode 100644 index 00000000..86b88df1 --- /dev/null +++ b/src/timeConversion.js @@ -0,0 +1,34 @@ +export default timeConversion + +/** +* Original Source: https://stackoverflow.com/questions/46883149 +* This method will calculate the time difference +* between two time values +* +* @param {String} startTime - string with the start time in hh:mm format +* @param {String} endTime - string with the end time in hh:mm format +* @return {String} - should return a string with the time difference in hh:mm format +*/ + +function timeConversion(time) { + + let convertedTime = time.substr(2, 6) + if (time.charAt(8) === 'A') { + if (time.charAt(0) === '1' && time.charAt(1) === '2') { + convertedTime = '00'.concat(convertedTime) + } else { + convertedTime = time.substr(0, 2) + convertedTime + } + } + if (time.charAt(8) === 'P') { + if (time.charAt(0) === '1' && time.charAt(1) === '2') { + convertedTime = '12'.concat(convertedTime) + } else { + const a = time.substr(0, 2) + const b = Number(a) + const c = (b + 12).toString() + convertedTime = c.concat(convertedTime) + } + } + return convertedTime +} diff --git a/test/timeConversion.test.js b/test/timeConversion.test.js new file mode 100644 index 00000000..1a6cee8c --- /dev/null +++ b/test/timeConversion.test.js @@ -0,0 +1,16 @@ +import test from 'ava' +import timeConversions from '../src/timeConversion' + +test('returns the time in 24 hours format', time => { + const timeGiven = "01:12:48AM" + const expectedTime = "01:12:48" + const actualTime = timeConversions(timeGiven) + time.is(actualTime, expectedTime) + }) + + test('returns the time in 24 hours format', time => { + const timeGiven = "06:48:56PM" + const expectedTime = "18:48:56" + const actualTime = timeConversions(timeGiven) + time.is(actualTime, expectedTime) + }) \ No newline at end of file