Coding Away Coding Away - 2 months ago 13
AngularJS Question

Angular with Javascript convert yyyy-mm-ddTHH:MM:SS.ms ( 2016-08-26T16:02:15.747 ) to m/d/yyyy hh:mm:ss am/pm 8/26/2016 9:02:15 AM

I am wanting to covert this "T" type of code

Currently

2016-08-26T16:02:15.747


I gather that is
yyyy-mm-ddTHH:MM:SS.ms
?

NEED TO convert with javascript to

8/26/2016 9:02:15 AM


( so I assume its like m/d/yyyy hh:mm:ss am/pm somehow )

I was looking at

Answer

If your time zone is GMT-07, then this will work:

var str = new Date('2016-08-26T16:02:15.747').toLocaleString('en-US');

Edit: As in comments you indicate your time zone is Arizona, the above will do. Arizona is GMT-07.

But if you are in another time zone, you'd need to provide that time zone as the second argument to toLocaleString. But as support for that is very limited, you could try to get around that by modifying the date based on your time zone:

d = new Date('2016-08-26T16:02:15.747');
d.setUTCMinutes(d.getUTCMinutes(d)+60*7-d.getTimezoneOffset());
s = d.toLocaleString('en-US');

However, this still may be 1 hour off when daylight saving is in effect.

... You probably would benefit from using momentjs which has many useful functions for dealing with this kind of manipulation.

Comments