Nitin
Nitin

Reputation: 528

Javascript 'yyyy-mm-dd hh:mm:ss' to timestamp

I want to convert datetime like '2015-05-01 05:13:43' into timestamp. Is there Any way to do it using JavaScript?

Upvotes: 3

Views: 4554

Answers (2)

hsz
hsz

Reputation: 152226

With pure JS you can try with:

new Date(Date.parse('2015-05-01 05:13:43+0000')).getTime() / 1000

It's important to add +0000 at the end of the string - otherwise browser will use your local timezone and add/remove few hours from the result.

getTime method gives you time in ms - so we have to divide it by 1000.

Upvotes: 4

Carmelo Catalfamo
Carmelo Catalfamo

Reputation: 93

You can do it!

let dateToConvert = '2015-05-01 05:13:43'
let date = new Date(dateToConvert)
let timestamp = date.getTime()

Upvotes: 1

Related Questions