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 2 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
33 changes: 33 additions & 0 deletions src/timeConversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default timeConversions

/**
*
* This method will convert time in 12 hours format to 24 hours format
*
* @param {String} time - time in 12 hours format example 01:12:45AM or 06:48:56PM
* @return {String} - converted time in 24 hours example 01:12:45 or 18:48:56
*/

function timeConversions(time) {

let convertedTime= time.substr(2, 6)
if(time.charAt(8)==='A'){
if(time.charAt(0)==='1' && time.charAt(1)==='2'){
convertedTime= '00'+convertedTime
}
else
convertedTime= time.substr(0, 2)+convertedTime
}
else{
if(time.charAt(0)==='1' && time.charAt(1)==='2'){
convertedTime= '12'+convertedTime
}
else{
let a = time.substr(0, 2)
let b = Number(a)
let c = (b+12)+''
convertedTime= c+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)
})