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?
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.
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With