3

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

Swapnil Yeole
  • 386
  • 2
  • 10
  • 23

2 Answers2

4

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.

hsz
  • 143,040
  • 58
  • 252
  • 308
1

You can do it!

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