Skip to content

Instantly share code, notes, and snippets.

@keif
Last active August 28, 2021 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keif/56ce238ac72c9d5ae0daaed117baec7d to your computer and use it in GitHub Desktop.
Save keif/56ce238ac72c9d5ae0daaed117baec7d to your computer and use it in GitHub Desktop.
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format. timeConversion has the following parameter(s): string s: a time in hour format Returns string: the time in hour format Input Format A single string that represents a time in -hour clock format (i.e.: or ). Constr…
const timeConversion = (s) => {
const seperator = `:`
const timeArr = s.slice(0, 8).split(seperator);
const hours = parseInt(timeArr[0], 10);
if (s.toUpperCase().indexOf(`PM`) > -1) {
// handle PM
timeArr[0] = (hours < 12) ? (hours + 12).toString() : hours.toString()
} else {
// handle AM
timeArr[0] = (hours === 12) ? "00" : timeArr[0].toString()
}
console.log(timeArr.join(seperator))
return timeArr.join(seperator)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment