Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a date in Epoch to "Y-m-d H:i:s" in Javascript?

Tags:

How can I convert following date in epoch:

1293683278

to following readable date:

2010-06-23 09:57:58

using Javascript?

Thanks!

like image 968
James Cazzetta Avatar asked May 10 '12 14:05

James Cazzetta


People also ask

How do I convert epoch to date?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy: Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .


2 Answers

var timestamp = 1293683278;  var date = new Date(timestamp * 1000);    var year = date.getFullYear();  var month = date.getMonth() + 1;  var day = date.getDate();  var hours = date.getHours();  var minutes = date.getMinutes();  var seconds = date.getSeconds();    console.log(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);

See js Date docs for further details

Another way:

var timestamp = 1293683278;  var date = new Date(timestamp * 1000);  var iso = date.toISOString().match(/(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2})/)    console.log(iso[1] + ' ' + iso[2]);
like image 66
deadrunk Avatar answered Sep 20 '22 16:09

deadrunk


You can use moment.js for this.

moment.unix(1293683278).format('YYYY-MM-DD HH:mm:ss'); 
like image 34
timrwood Avatar answered Sep 20 '22 16:09

timrwood