Skip to content

Instantly share code, notes, and snippets.

@ivantsov
Created April 9, 2017 20:05
Show Gist options
  • Save ivantsov/fe33ae2f51e2fb08c17cc8efbf64d07a to your computer and use it in GitHub Desktop.
Save ivantsov/fe33ae2f51e2fb08c17cc8efbf64d07a to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/jimebuw
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*
Given a time in 12-hour AM/PM format, convert it to 24-hour format.
Input format:
A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01 <= hh <= 12 and 00 <= mm, ss <= 59.
Output format:
A single string in 24-hour format, where 00 <= hh <= 23.
*/
function convert(time) {
const hours = parseInt(time.substring(0, 2), 10);
const dayTime = time.substring(time.length - 2, time.length);
let convertedTime = time.replace(dayTime, '');
let convertedHours = hours;
if (dayTime === 'AM' && hours === 12) {
convertedHours = 0;
}
else if (dayTime === 'PM' && hours !== 12) {
convertedHours = hours + 12;
}
if (convertedHours < 10) {
convertedHours = `0${convertedHours}`;
}
console.log(`${convertedHours}${convertedTime.substring(2, convertedTime.length)}`);
}
convert('07:01:00AM');
convert('12:30:12AM');
convert('12:30:12PM');
convert('03:30:12AM');
convert('03:30:12PM');
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
Given a time in 12-hour AM/PM format, convert it to 24-hour format.
Input format:
A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01 <= hh <= 12 and 00 <= mm, ss <= 59.
Output format:
A single string in 24-hour format, where 00 <= hh <= 23.
*/
function convert(time) {
const hours = parseInt(time.substring(0, 2), 10);
const dayTime = time.substring(time.length - 2, time.length);
let convertedTime = time.replace(dayTime, '');
let convertedHours = hours;
if (dayTime === 'AM' && hours === 12) {
convertedHours = 0;
}
else if (dayTime === 'PM' && hours !== 12) {
convertedHours = hours + 12;
}
if (convertedHours < 10) {
convertedHours = `0${convertedHours}`;
}
console.log(`${convertedHours}${convertedTime.substring(2, convertedTime.length)}`);
}
convert('07:01:00AM');
convert('12:30:12AM');
convert('12:30:12PM');
convert('03:30:12AM');
convert('03:30:12PM');</script></body>
</html>
/*
Given a time in 12-hour AM/PM format, convert it to 24-hour format.
Input format:
A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01 <= hh <= 12 and 00 <= mm, ss <= 59.
Output format:
A single string in 24-hour format, where 00 <= hh <= 23.
*/
function convert(time) {
const hours = parseInt(time.substring(0, 2), 10);
const dayTime = time.substring(time.length - 2, time.length);
let convertedTime = time.replace(dayTime, '');
let convertedHours = hours;
if (dayTime === 'AM' && hours === 12) {
convertedHours = 0;
}
else if (dayTime === 'PM' && hours !== 12) {
convertedHours = hours + 12;
}
if (convertedHours < 10) {
convertedHours = `0${convertedHours}`;
}
console.log(`${convertedHours}${convertedTime.substring(2, convertedTime.length)}`);
}
convert('07:01:00AM');
convert('12:30:12AM');
convert('12:30:12PM');
convert('03:30:12AM');
convert('03:30:12PM');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment