How To Convert Milliseconds To Date And Time?
I know this question has been asked and answered many times, but I could not convert a given milliseconds to date and time. I have the following code: var date = '1475235770601'; v
Solution 1:
Date
's constructor takes a number, not a string. Either feed it in directly:
date = 1475235770601; // Note the lack of quotes making it a number
Or, if you already have a string, convert it explicitly:
date = parseInt(date);
Solution 2:
I ended up changing my code like this:
var date = '1475235770601';
var d = newDate(parseInt(date, 10));
var ds = d.toString('MM/dd/yy HH:mm:ss');
console.log(ds);
Output:
09/30/16 17:12:50
Solution 3:
I got quite a reasonable result with:
const rawDate = newDate(milliseconds)
constdate = rawDate.toLocaleDateString() + " " + rawDate.toLocaleTimeString()
This outputs date and time in a locally accepted format (depending on user system datetime settings, I guess)!
So if you need just a locally accepted format (and do not strictly prefer slashes in the date) - you can use that.
Post a Comment for "How To Convert Milliseconds To Date And Time?"