How can I convert normal date 2012.08.10
to unix timestamp in javascript?
Fiddle: http://jsfiddle.net/J2pWj/
I've seen many posts here that convert it in PHP, Ruby, etc... But I need to do this inside JS.
How can I convert normal date 2012.08.10
to unix timestamp in javascript?
Fiddle: http://jsfiddle.net/J2pWj/
I've seen many posts here that convert it in PHP, Ruby, etc... But I need to do this inside JS.
It's important to add the
toFixed(0)
to remove any decimals when dividing by 1000 to convert from milliseconds to seconds.The
.getTime()
function returns the timestamp in milliseconds, but true unix timestamps are always in seconds.In this case it's important to return only a whole number (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses
Math.floor()
and notMath.round()
).You can do it using Date.parse() Method.
Date.parse("03.03.2016") output-> 1456959600000
Date.parse("2015-12-12") output-> 1449878400000
Check the JavaScript Date documentation.
You should check out the moment.js api, it is very easy to use and has lots of built in features.
I think for your problem, you could use something like this: