Reputation: 11871
I have this time: 02:00:00
, how would I format it 2:00pm
I have tried:
var time = new Date(z.app_time),
h = time.getHours(),
m = time.getMinutes();
but h returns NaN, same as m
Upvotes: 2
Views: 1598
Reputation: 175826
One way:
var time = "02:00:00".split(":"),
h = +time[0],
p;
if (h > 12) {
h -= 12;
p = "pm";
} else {
h = h || 12;
p = "am";
}
var t = h + ":" + time[1] + p;
Upvotes: 3
Reputation: 1045
If you want to get current date so you don't need to give any parameter to Date()
object.
Try this, it will give you the time as you want:
var time = new Date(),
h = time.getHours(),
m = time.getMinutes();
ampm = h >= 12 ? 'pm' : 'am';
var finalTime = h + ':' + m + ampm;
Hope this helps.
Upvotes: 0