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>
I don't see all your code, but if you can just get the value of milliseconds as
1373428800000
out of that json, then you can pass it to moment directly. I thinkformatDate
is a function you wrote. Does it do something important like manipulate time that you require of moment.js, or could you just use the format function of moment?This format is already supported natively by moment.js. Just pass it directly.
You can then use any of the moment functions, such as
.format()
or.toDate()
If you want UTC, then do:
Again, you can call
format
ortoDate
, however be aware thattoDate
will produce aDate
object, which will still have local time behaviors. Unless you absolutely need aDate
object, then you should stick withformat
and other moment functions.