Im not particularly skilled when it comes to working with datetimes in js. I have a JSON object that has some datetime fields formatted like YYYY-MM-DDTHH:MM:SS now I have to put this in HH:mm AM/PM and I came up with:
var d = Date.parse('2013-04-17T13:05:00');
var date = new Date(d);
var hrs = date.getUTCHours();
var min = date.getUTCMinutes();
var meridian;
if(hrs > 12){
hrs -= 12;
meridian = 'PM';
}
else {
meridian = 'AM';
}
if (min < 10) {
min = "0"+min;
};
var time = hrs+":"+min+" "+meridian;
Is there a better way to accomplish this?
3 Answers 3
try moment.js, this library has a ton of functionality built in.
moment(String, String);
moment("12-25-1995", "MM-DD-YYYY");
"hA" is the righ format for "3PM" format
2 Comments
A different answer for a different approach
has some datetime fields formatted like
YYYY-MM-DDTHH:MM:SSnow I have to put this inHH:mm AM/PM
As you know the formatting, you can take the parts you want directly from the datetime String, avoiding the use of a Date instance or library all together.
var stamp = '2013-04-17T13:05:00',
hrs = +stamp.slice(11, 13), // 13
min = +stamp.slice(14, 16); // 5
then continue as before from here.
Comments
This is overkill, but I recently went out of my way to make my own function just for things like this
var easyDate = (function () {
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
thstndrd = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'],
toDate = function () {return new Date(Date.UTC(this.yyyy, this.MM - 1, this.dd, this.hh, this.mm, this.ss, this.ms));},
toLocaleDate = function () {return new Date(this.yyyy, this.MM - 1, this.dd, this.hh, this.mm, this.ss, this.ms);},
UTC = function UTC(d) {
return {
ms: d.getUTCMilliseconds(),
ss: d.getUTCSeconds(),
mm: d.getUTCMinutes(),
hh: d.getUTCHours(),
dd: d.getUTCDate(),
MM: d.getUTCMonth() + 1,
yyyy: d.getUTCFullYear(),
dow: d.getUTCDay(),
toDate: toDate,
toLocaleDate: toLocaleDate
}
},
easyDate = function easyDate(d) {
var o = UTC(d);
return {
dd: '' + o.dd,
th: thstndrd[o.dd % 10],
day: days[o.dow],
MM: '' + o.MM,
month: months[o.MM - 1],
year: '' + o.yyyy,
am: o.hh < 12 ? 'a.m.' : 'p.m.',
hh: o.hh < 10 ? '0' + o.hh : '' + o.hh,
h: '' + (o.hh % 12 || 12),
mm: o.mm < 10 ? '0' + o.mm : '' + o.mm,
ss: o.ss < 10 ? '0' + o.ss : '' + o.ss,
ms: ('00' + o.ms).slice(-3),
UTC: o
};
};
easyDate.UTC = UTC;
easyDate.delta = function (then, now) {
var o, p, dir = -1;
now || (now = new Date());
if (now >= then) o = UTC(now), p = UTC(then);
else o = UTC(then), p = UTC(now), now = then, dir = 1;
o.dir = dir;
o.dow = p.dow;
o.ms = o.ms - p.ms;
o.ss = o.ss - p.ss;
o.mm = o.mm - p.mm;
o.hh = o.hh - p.hh;
o.dd = o.dd - p.dd;
o.MM = o.MM - p.MM;
o.yyyy = o.yyyy - p.yyyy;
// fix negatives
if (o.ms < 0) --o.ss, o.ms = (o.ms + 1000) % 1000;
if (o.ss < 0) --o.mm, o.ss = (o.ss + 60) % 60;
if (o.mm < 0) --o.hh, o.mm = (o.mm + 60) % 60;
if (o.hh < 0) --o.dd, o.hh = (o.hh + 24) % 24;
if (o.dd < 0) { // months have different lengths
--o.MM;
now.setUTCMonth(now.getUTCMonth() + 1);
now.setUTCDate(0);
o.dd = (o.dd + now.getUTCDate()) % now.getUTCDate();
}
if (o.MM < 0) --o.yyyy, o.MM = (o.MM + 12) % 12;
return o;
};
return easyDate;
}());
And using it
var o = easyDate(new Date());
o.month + ' ' + o.dd + o.th + ', ' + o.h + ':' + o.mm + ' ' + o.am;
// "June 18th, 7:30 p.m."
If you noticed the thing called easyDate.delta in the code, yep, it supports calculating differences in time, too. If you just want to run getUTC* for everything then you have easyDate.UTC. :)
5 Comments
Is there a better way to accomplish this? to be about formatting Date (or skipping Date entirely), not about how to convert ISO 8601 to Date.YYYY-MM-DDTHH:MM:SS to a Date without error in 2 lines. Yes, it could be done in one with just a new Date('2013年04月17日T13:05:00');, but he's demonstrated that he is able to parse it.Explore related questions
See similar questions with these tags.
.toLocaleFormatmethod, but alas, it only works in Firefox. You'd need to use a library if you want something easier, but unless you need to juggle timezones it's usually more efficient to just write your own formatter for your particular case.