I want to convert datetime like '2015-05-01 05:13:43' into timestamp. Is there Any way to do it using JavaScript?
Asked
Active
Viewed 4,072 times
3
-
you can try `moment.js` https://momentjs.com/docs/#/parsing/string/ – wdetac Apr 25 '18 at 09:41
-
I want a solution in core javascript without any library like 'moment.js' – Swapnil Yeole Apr 25 '18 at 09:42
-
Can you at least show us what you've tried so far? – Xoog Apr 25 '18 at 09:43
-
Possible duplicates of https://stackoverflow.com/questions/9873197/convert-date-to-timestamp-in-javascript – Kishor Jadhav Apr 25 '18 at 09:55
-
@KishorJadhav, It is not working for scenario like 'mm;hh:ss' – Swapnil Yeole Apr 25 '18 at 09:56
2 Answers
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
- 148,279
- 62
- 259
- 315
1
You can do it!
let dateToConvert = '2015-05-01 05:13:43'
let date = new Date(dateToConvert)
let timestamp = date.getTime()

Carmelo Catalfamo
- 93
- 1
- 4
- 15