Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

adding time conversion function #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/timeConversion.js
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions test/timeConversion.test.js
Original file line number Diff line number Diff line change
@@ -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)
})