Reputation: 583
What is the best way to convert military time to am and pm time. . I have the following code and it works fine:
$scope.convertTimeAMPM = function(time){
//var time = "12:23:39";
var time = time.split(':');
var hours = time[0];
var minutes = time[1];
var seconds = time[2];
$scope.timeValue = "" + ((hours >12) ? hours -12 :hours);
$scope.timeValue += (minutes < 10) ? ":0" : ":" + minutes;
$scope.timeValue += (seconds < 10) ? ":0" : ":" + seconds;
$scope.timeValue += (hours >= 12) ? " P.M." : " A.M.";
//console.log( timeValue);
}
But I am not satisfied in the output shows when I run my program. .
Sample output:
20:00:00 8:0:0 P.M.
08:00:00 08:0:0 A.M
16:00:00 4:30:0 P.M.
I want to achieve the output which looks like the following:
20:00:00 8:00:00 P.M.
08:00:00 8:00:00 A.M
16:30:00 4:30:00 P.M.
Is there any suggestions there? Thanks
Upvotes: 11
Views: 33579
Reputation: 1
How about this as a helper function?
let convertMilitaryToStandard = function(time) {
let timeParts = time.split(":");
let standardTime = "";
if (parseInt(timeParts[0]) > 12) {
timeParts[0] = timeParts[0] - 12;
standardTime = timeParts.join(":") + " PM";
} else if (parseInt(timeParts[0]) === 12) {
standardTime = timeParts.join(":") + " PM";
} else {
standardTime = timeParts.join(":") + " AM";
}
return standardTime;
}
console.log(convertMilitaryToStandard("12:15:12"));
console.log(convertMilitaryToStandard("01:15:12"));
console.log(convertMilitaryToStandard("18:15"));
Upvotes: 0
Reputation: 663
The function toLocaleTimeString()
did it for me.
From the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
// Depending on timezone, your results will vary
const event = new Date('August 19, 1975 23:15:30 GMT+00:00');
console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM
Upvotes: 0
Reputation: 49
As an extension to Huy Hoang Pham's answer, here's the code with Luxon rather than moment.js (see this and this)
import { DateTime } from "luxon";
function convert(input) {
return DateTime.fromFormat(input, 'HH:mm:ss').toFormat('h:mm:ss A');
}
console.log(convert('20:00:00'));
console.log(convert('08:00:00'));
console.log(convert('16:30:00'));
Time is complicated, and I would recommend using a library over your own code for conversions like this.
Upvotes: 3
Reputation: 349
const parseMillitaryTime = (time: string) =>
pipe(
time,
s => s.split(':'),
time_ =>
`${((Number(time_[0]) + 11) % 12) + 1}:${
Number(time_[1]) < 10 ? `0${Number(time_[1])}` : `${Number(time_[1])}`
} ${Number(time_[0]) > 11 ? 'pm' : 'am'}`
)
Upvotes: 0
Reputation: 21
A compact way to do it using ES6 can be the following:
toRegularTime = (militaryTime) => {
const [hours, minutes, seconds] = militaryTime.split(':');
return `${(hours > 12) ? hours - 12 : hours}:${minutes}${seconds ? `:${seconds}` : ''} ${(hours >= 12) ? 'PM' : 'AM'}`;
}
Upvotes: 1
Reputation: 980
No need for a library 🙂
Here's a sweet & simple function I whipped up that should suit your needs exactly
function toStandardTime(militaryTime) {
militaryTime = militaryTime.split(':');
return (militaryTime[0].charAt(0) == 1 && militaryTime[0].charAt(1) > 2) ? (militaryTime[0] - 12) + ':' + militaryTime[1] + ':' + militaryTime[2] + ' P.M.' : militaryTime.join(':') + ' A.M.'
}
console.log(toStandardTime('16:30:00'));
And a more readable version in case it's needed
function toStandardTime(militaryTime) {
militaryTime = militaryTime.split(':');
if (militaryTime[0].charAt(0) == 1 && militaryTime[0].charAt(1) > 2) {
return (militaryTime[0] - 12) + ':' + militaryTime[1] + ':' + militaryTime[2] + ' P.M.';
} else {
return militaryTime.join(':') + ' A.M.';
}
}
console.log(toStandardTime('16:30:00'));
Upvotes: 1
Reputation: 15501
You missed concatenating the string when minutes < 10
and seconds < 10
so you were not getting the desired result.
Convert string to number using Number()
and use it appropriately as shown in the working code snippet below:
EDIT: Updated code to use Number()
while declaration of hours
, minutes
and seconds
.
var time = "16:30:00"; // your input
time = time.split(':'); // convert to array
// fetch
var hours = Number(time[0]);
var minutes = Number(time[1]);
var seconds = Number(time[2]);
// calculate
var timeValue;
if (hours > 0 && hours <= 12) {
timeValue= "" + hours;
} else if (hours > 12) {
timeValue= "" + (hours - 12);
} else if (hours == 0) {
timeValue= "12";
}
timeValue += (minutes < 10) ? ":0" + minutes : ":" + minutes; // get minutes
timeValue += (seconds < 10) ? ":0" + seconds : ":" + seconds; // get seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."; // get AM/PM
// show
alert(timeValue);
console.log(timeValue);
Read up: Number()
| MDN
Upvotes: 18
Reputation: 4147
As Nit recommend, Moment.js provides a simple solution to your problem.
function convert(input) {
return moment(input, 'HH:mm:ss').format('h:mm:ss A');
}
console.log(convert('20:00:00'));
console.log(convert('08:00:00'));
console.log(convert('16:30:00'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.js"></script>
Upvotes: 12