How to Convert Milliseconds to Date in JS?
12,171
Solution 1
var d = new Date(+milliseconds);
Solution 2
This should work:
var d = new Date(milliseconds);
Author by
user3592479
Updated on August 04, 2022Comments
-
user3592479 almost 2 years
My scenario is to convert the data of milliseconds to a date format. I have tried a lot as here http://jsbin.com/jeququ/1/ but it is only working for a limited milliseconds value, and fails if the value is high. I am looking to replicate the functionality as http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/#.VJAp9lWUe1R
Milliseconds Since : Thu Jan 01 1970 05:30:00 GMT+0530
-
SomeShinyObject over 9 yearspossible duplicate of Converting milliseconds to a date (jQuery/JS)
-
Claudio Redi over 9 yearsMilliseconds since when? Unix Epoch?
-
-
user3592479 over 9 yearsI am already using the same . But how do we take a reference date . Required reference date is Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
-
Timespace over 9 years@user3592479 Your reference date is the same as 1st January 1970 00:00:00 UTC, so this will work fine for you.
-
user3592479 over 9 years@userTimespace , but how can we take any other date as reference
-
Timespace over 9 years@user3592479 As far as I know, there isn't a function built in to JavaScript that lets you do that. You could write your own, or find a library that lets you do that. Though this is different enough from your original question that I'd advise asking a new question if you need to use a different reference date (and someone else might prove me wrong!)
-
user3592479 over 9 yearsOk. But is there any significance to take 1970 year as default?
-
Timespace over 9 years@user3592479 Yes - 1st January 1970 00:00:00 UTC is the Unix Epoch. When expressing time as an integer in most programming languages,
0
is the Unix Epoch (and if you don't want this to be the case, you'll probably end up making your life more difficult than it needs to be). If your question is why this is the default, perhaps this question can help. -
The1993 about 4 yearsCan someone explain what's the difference between using + and omitting it?
-
Leonard Arnold over 3 years@The1993 If the variable is not a number but e.g. a string you have to use the + operator. It behaves like a parseInt.