Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert milliseconds into a readable date?

The following:

new Date(1324339200000).toUTCString()

Outputs:

"Tue, 20 Dec 2011 00:00:00 GMT"

I need it to return Dec 20. Is there a better method I can use besides toUTCString()? I am looking for any way to parse through milliseconds, to return a human readable date.

like image 470
Christian Fazzini Avatar asked Dec 20 '11 18:12

Christian Fazzini


People also ask

How do you convert milliseconds to dates in Excel?

If you need milliseconds, you need to add a further multiplication / division by 1000. For example, converting from epoch time (milliseconds) to a date would be "=((((A1/1000)/60)/60)/24)+DATE(1970,1,1)".

How do you convert milliseconds to time?

To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.

What is milliseconds equal to?

A millisecond (from milli- and second; symbol: ms) is a unit of time in the International System of Units (SI) equal to one thousandth (0.001 or 10−3 or 1/1000) of a second and to 1000 microseconds.


3 Answers

Using the library Datejs you can accomplish this quite elegantly, with its toString format specifiers: http://jsfiddle.net/TeRnM/1/.

var date = new Date(1324339200000);  date.toString("MMM dd"); // "Dec 20" 
like image 116
pimvdb Avatar answered Sep 19 '22 18:09

pimvdb


You can use datejs and convert in different formate. I have tested some formate and working fine.

var d = new Date(1469433907836); // Parameter should be long value

d.toLocaleString()     // 7/25/2016, 1:35:07 PM
d.toLocaleDateString() // 7/25/2016
d.toDateString()       // Mon Jul 25 2016
d.toTimeString()       // 13:35:07 GMT+0530 (India Standard Time)
d.toLocaleTimeString() // 1:35:07 PM
d.toISOString();       // 2016-07-25T08:05:07.836Z
d.toJSON();            // 2016-07-25T08:05:07.836Z
d.toString();          // Mon Jul 25 2016 13:35:07 GMT+0530 (India Standard Time)
d.toUTCString();       // Mon, 25 Jul 2016 08:05:07 GMT
like image 24
Ravindra Avatar answered Sep 19 '22 18:09

Ravindra


No, you'll need to do it manually.

function prettyDate(date) {
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  return months[date.getUTCMonth()] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}

console.log(prettyDate(new Date(1324339200000)));
like image 35
lonesomeday Avatar answered Sep 19 '22 18:09

lonesomeday