Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Convert Date Time string to Epoch

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.

like image 447
cbm64 Avatar asked Dec 04 '12 16:12

cbm64


People also ask

How to convert Date time to epoch in JavaScript?

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.

How do I convert time to epoch?

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

How to get epoch Date in JavaScript?

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 do you convert a string to a number in JavaScript?

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.


2 Answers

var someDate = new Date(dateString); someDate = someDate.getTime(); 
like image 152
Crayon Violent Avatar answered Oct 06 '22 01:10

Crayon Violent


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 
like image 28
melvs Avatar answered Oct 06 '22 01:10

melvs