2

I have my time since epoch stored as a number: 1444749469000. However, when I try to convert it to a Date object, using Date(1444749469000), it just gives me the current date instead of the one it should be (around Tue Oct 13 2015).


> Date(1444749469000)
"Tue Apr 12 2016 09:28:30 GMT-0700 (PDT)"
  • Possible duplicate of [Converting milliseconds to a date (jQuery/JS)](http://stackoverflow.com/questions/4673527/converting-milliseconds-to-a-date-jquery-js) – djechlin Apr 12 '16 at 16:35
  • @A.J. doesn't look like the OP checked that link to me. – djechlin Apr 12 '16 at 16:48

2 Answers2

3

You need a new before the Date because Date is a constructor:

var d = new Date(1444749469000)
alert(d);
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • By definition, constructors **are** functions. ;-) The difference is how they're called. – RobG Apr 13 '16 at 00:25
1

Because when you call Date as a function, it will return a string of current date and ignore the given value. In order to retrieve the Date object, you must initialize the Date constructor with keyword new.

var now = Date(1444749469000);
var date = new Date(1444749469000);
console.log(typeof now); //string
console.log(typeof date); //object
Lewis
  • 14,132
  • 12
  • 66
  • 87