so I give up...been trying to do this all day;
I have a string that supplies a date and time in the format dd/MM/yyyy hh:mm
(04/12/2012 07:00
).
I need to turn that into an Epoch date so I can do some calculations upon it. I cannot modify the format in which the date time is sent to me.
JavaScript or jQuery is fine.
var time = new Date(). getTime() / 1000 + 900 + 330*60; console. log("time = "+time); getTime() will return current time with milleseconds in last 3 digit so divide it by 1000 first.
Multiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.
The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number of seconds since epoch.
How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.
var someDate = new Date(dateString); someDate = someDate.getTime();
You can use Date.parse(date)
.
function epoch (date) { return Date.parse(date) } const dateToday = new Date() // Mon Jun 08 2020 16:47:55 GMT+0800 (China Standard Time) const timestamp = epoch(dateToday) console.log(timestamp) // => 1591606075000
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