Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript convert from epoch string to Date object

var myDate = new Date(); var epoch = myDate.getTime(); //1318023197289 number of ms since epoch var unixEpoch = Math.round(epoch/1000) 
  1. How do you convert epoch back to a Date object?
  2. Can you also convert unixEpoch back to a Date object?
like image 923
fortuneRice Avatar asked Oct 07 '11 21:10

fortuneRice


People also ask

How do I convert epoch to Date?

Because our Epoch time is specified in milliseconds, we may convert it to seconds. To convert milliseconds to seconds, first, divide the millisecond count by 1000. Later, we use DATEADD() to add the number of seconds since the epoch, which is January 1, 1970 and cast the result to retrieve the date since the epoch.

How do you convert a string to a Date in JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How do I convert epoch to GMT?

=(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).


1 Answers

var date = new Date(1318023197289); 

And, since unixEpoch is simply epoch / 1000, you can similarly multiply the argument in the constructor by 1000.

like image 82
Matt Avatar answered Sep 20 '22 03:09

Matt