Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Microsoft JSON Date to Local DateTime Using Moment JS Only

JSON Date: '/Date(1373428800000)/' End Result: 7/9/2013 8:00 PM EST

Currently I do it in 3 steps:

var a = cleanJsonDate('JsonDate');
var b = formatDate(a); // 7/10/2013 12:00 AM
var c = moment.utc(b); // 7/9/2013 8:00 PM
return c;

Is it possible to accomplish the same result using moment js only?

----Update-----

Combining @ThisClark & @Matt answers. I came as close as possible to the goal; however, the 'h' format does not work for some reason, I still get 20.00.00 instead of 8:00

var m = moment.utc(moment('/Date(1373428800000)/').format('M/D/YYYY h:m A')).toDate();
alert(m);
<script src="http://momentjs.com/downloads/moment.min.js"></script>
like image 721
usefulBee Avatar asked Dec 02 '22 14:12

usefulBee


1 Answers

This format is already supported natively by moment.js. Just pass it directly.

moment('/Date(1373428800000)/')

You can then use any of the moment functions, such as .format() or .toDate()

If you want UTC, then do:

moment.utc('/Date(1373428800000)/')

Again, you can call format or toDate, however be aware that toDate will produce a Date object, which will still have local time behaviors. Unless you absolutely need a Date object, then you should stick with format and other moment functions.

like image 55
Matt Johnson-Pint Avatar answered Dec 04 '22 02:12

Matt Johnson-Pint