new Date(milliseconds) returns Invalid date
Asked Answered
J

5

56

I am trying to convert milliseconds to a date using the javascript using:

new Date(Milliseconds); 

constructor, but when I give it a milliseconds value of say 1372439683000 it returns invalid date. If I go to a site that converts milliseconds to date it returns the correct date.

Any ideas why?

Jauregui answered 28/6, 2013 at 18:26 Comment(2)
alert(new Date(1372439683000)); works for me. What do you mean by "invalid date" exactly, what result are you getting?Anatolio
@Pekka웃 The OP is getting a Date object that stringifies to "Invalid Date". (e.g., try out alert(new Date("")))Tongs
T
115

You're not using a number, you're using a string that looks like a number. According to MDN, when you pass a string into Date, it expects

a format recognized by the parse method (IETF-compliant RFC 2822 timestamps).

An example of such a string is "December 17, 1995 03:24:00", but you're passing in a string that looks like "1372439683000", which is not able to be parsed.

Convert Milliseconds to a number using parseInt, or a unary +:

new Date(+Milliseconds); 
new Date(parseInt(Milliseconds,10)); 
Tongs answered 28/6, 2013 at 18:30 Comment(7)
new Date(929397621000) in Developer Tools returns a validly formatted date string Mon Jun 14 1999 15:00:21 GMT-0700 (PDT) yet when I try to inspect the object, it shows "Invalid Date". And in the actual code where this is being used, it also creates an invalid date.Sabah
@Sabah This appears to be a different issue from the one described here. Are you doing console.dir(new Date(929397621000))? If so, seeing __proto__: Invalid Date inside that instance is correct (or at least unsurprising) behavior. The Date prototype is an invalid date; valid date information exists on Date instances, not on the prototype.Tongs
I'm entering the expression directly as a watch in Chrome's developer tools. My code is basically doing the same thing. Since I'm calling new that should produce a valid Date object, yes? Strangely, even new Date() is producing an invalid date (that the debugger can strangely stringify correctly) which I cannot inspect the elements of (because it only shows "Invalid Date") so maybe it is a cockpit issue.Sabah
Well, it appears that the problem only occurs in the inspector... the actual object is valid, even though inspecting the object shows it is invalid.Sabah
@Sabah Your issues appears to be nuanced enough to merit a new question post (or bug report?)Tongs
nah, it turned out the problem was a string being passed in (for some reason, the variable was supposed to be a number, but why it isn't is for a separate debug session) but then after i fixed it i was trying to verify it in the debugger, and ran across this Chrome issue/quirk - doesn't really cause any harm, just made me think the bug wasn't fixed.Sabah
In my case I was fetching the data and it came as 1.495462321562E12. It was a string and hence I tried to convert it to int and it failed. The actual thing to do there was parseFloat().Hypopituitarism
P
5

The Date function is case-sensitive:

new Date(Milliseconds); 
Perfecto answered 28/6, 2013 at 18:29 Comment(0)
C
2

Is important to note that the timestamp parameter MUST be a number, it cannot be a string.

new Date(1631793000000).toLocaleString('en-GB', { timeZone: 'America/Argentina/Buenos_Aires' });

// Returns: '16/09/2021, 08:50:00'

new Date("1631793000000").toLocaleString('en-GB', { timeZone: 'America/Argentina/Buenos_Aires' });

// Returns: 'Invalid Date'

In case you're receiving the timestamp as string, you can simply wrap it around parseInt(): parseInt(your_ts_string)

Christenechristening answered 16/9, 2021 at 11:46 Comment(0)
R
1

instead of this

new date(Milliseconds); 

use this

new Date(Milliseconds); 

your statement will give you date is not defined error

Ruthy answered 28/6, 2013 at 18:29 Comment(0)
B
0

I was getting this error due to a different reason.

I read a key from redis whose value is a json.

client.get(someid, function(error, somevalue){});

Now i was trying to access the fields inside somevalue (which is a string), like somevalue.start_time, without parsing to JSON object. This was returning "undefined" which if passed to Date constructor, new Date(somevalue.start_time) returns "Invalid date".

So first using JSON.parse(somevalue) to get JSON object before accessing fields inside the json solved the problem.

Baccy answered 21/9, 2015 at 8:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.