Moment.js and Unix Epoch Conversion

I have a web service that is returning a date as the following string:


I use MomentJS to parse this to a moment object.

moment("/Date(1377907200000)/") => Fri Aug 30 2013 20:00:00 GMT-0400

All of that is fine. But when I call unix() on the object I am given the value 1377907200. This, however, corresponds to Fri Jan 16 1970 17:45:07 GMT-0500. I could just multiply the value returned by unix() but that seems sloppy to me. I suspect that what I am doing by calling unix() is not exactly what I think it is. Do I need to specify some sort of format when calling unix()? What am I missing here?

JSFidle showing the conversion to moment and then back.


The answer provided by meagar is correct, from strictly a JavaScript / Unix time perspective. However, if you just multiply by 1000, you will loose any sub-second precision that might have existed in your data.

Moment.js offers two different methods, as described in the docs. .unix() returns the value in seconds. It is effectively dividing by 1000 and truncating any decimals. You want to use the .valueOf() method, which just returns the milliseconds without modification.

Need Your Help

iphone app store phone contacts

iphone ios storage contact

We are developing an application in iOS (iPhone App) that is storing phone contacts in our server. We are planning to store phone number and email address of the contacts on the server(We have rest...

How to choose randomly in a certain ratio

algorithm random probability

I want to choose randomly* between two alternatives with unequal probability.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.