Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating milliseconds from epoch

Given that I have: mm, dd, yy, hh:mm, am/pm, what is the recommended/easiest way to convert that data into milliseconds from epoch?

like image 992
deruse Avatar asked Jan 25 '12 23:01

deruse


People also ask

Is epoch time in seconds or milliseconds?

In computing, Unix time (also known as Epoch time, Posix time, seconds since the Epoch, Unix timestamp or UNIX Epoch time) is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970.

How do you convert timestamps to milliseconds?

A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .

How do you convert time to epoch time?

Epoch Time Difference Formula Multiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

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.


1 Answers

Everytime I get an upvote on this answer, I'm reminded that my answer doesn't actually answer the question.

OP: If you can parse your string into the following format, you can use one of the other answers. MM/DD/YYYY HH:MM:SS AM EDT (It's important to specify the timezone; epoch is based on the meridian)

let milliseconds = new Date("1/20/1982 5:00 PM EST").getTime()  console.log(milliseconds);

Original Answer that some people have found useful: Another technique (which most browsers support, as well as NodeJS) that doesn't require having to instantiate a Date object

var nowEpoch = Date.now(); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

like image 89
Chris Avatar answered Sep 28 '22 16:09

Chris