I’ve got a chart library that’s expecting date values as though I had called:
var utc = Date.UTC(2012, 1, 15);
but my server is returning date strings. Given a date string of “2/15/2012” is there a simple way to get the same result of calling Date.UTC(2012, 1, 15); ?
What I have below works, but I’m wondering if there’s a simpler way.
//correct result I'm shooting for
var utc = Date.UTC(2012, 1, 15);
//is there an easier way?
var d = new Date("2/15/2012");
var utc2 = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate());
console.log(utc, " === ", utc2, (utc === utc2) ? "SWEET" : "DAMNIT");
If the date is always in
m/d/y(or, at least, doesn’t already have a timezone) format, you can append' UTC'to it and usegetTime: