Convert normal date to unix timestamp
Solution 1
Math.floor(new Date('2012.08.10').getTime() / 1000)
Check the JavaScript Date documentation.
Solution 2
parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))
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.
Solution 3
var d = '2016-01-01T00:00:00.000Z';
console.log(new Date(d).valueOf()); // returns the number of milliseconds since the epoch
Solution 4
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:
var unixTimestamp = moment('2012.08.10', 'YYYY.MM.DD').unix();
Solution 5
You can do it using Date.parse() Method.
Date.parse($("#yourCustomDate).val())
Date.parse("03.03.2016") output-> 1456959600000
Date.parse("2015-12-12") output-> 1449878400000
Related videos on Youtube
Stan
Updated on July 15, 2022Comments
-
Stan almost 2 years
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.-
Martin York almost 12 yearspossible duplicate of convert date to timestamp in javascript?
-
Gareth Parker almost 12 yearsHave you tried newDate.getTime() / 1000?
-
Stan almost 12 years@LokiAstari yes, sorry. I did search but didn't find that.
-
Michal Stefanow about 5 yearsMaybe I'm missing something but
unix timestamp
is so fundamental to all the engineering and computer science. Wish there was built-in convenience method. Currently I'm usingMath.floor((+new Date()) / 1000);
-
Ank_247shbm about 2 yearsThis worked fine: stackoverflow.com/a/72047159/8119511
-
-
B T almost 10 yearsMath.floor it or its not a unix timestamp (it'll have a decimal)
-
WP Learner over 8 years@fguillen,Mooseman, I run and check this code. Once I convert unix timestamp to date it is giving me 2012.08.09 instead of 2012.08.10. Why is that..?
-
theVinchi over 8 years@user2584538, see my answer below. You must remove the decimals.
-
Capaj almost 8 yearsjust beware that this return a string, not a number.
-
theVinchi over 7 yearsGood point, changed answer to wrap with parseInt() to convert back to integer.
-
WeizhongTu over 7 yearsWhy does Date.parse give incorrect results?: stackoverflow.com/a/2587398/2714931
-
Capricorn almost 6 yearsHow does this answer differentiate from the one given by Barmar 6 years ago?
-
Mentor about 5 yearsDid something change since this answer?
.getTime()
returns UNIX timestamps at the time of writing this... -
Laurens almost 5 yearsJust want to add two comments here: the unary add + operator is not needed, as
/
is already a math operator. Also,toFixed
returns a string and not a number, in case the type is important.. -
Nicu Surdu almost 5 years@Mentor UNIX time is measured in seconds, not milliseconds
-
Shahin Ghasemi about 4 yearswhy did you divide the result to 1000? because
getTime
will give you in ms -
mindplay.dk almost 3 yearsIf performance matters, probably the better and more correct option is
Math.round
orMath.floor
, rather than converting to a string and then back to an integer - that is:Math.round(new Date('2012.08.10').getTime() / 1000)
-
kasvith about 2 yearsshould be the accepted answer
-
jaquinocode about 2 yearsI haven't tested this out but just putting my two cents in here that I'm thinking this would be very slow compared to the current top method of
Math.floor(new Date('2012.08.10').getTime() / 1000)
. Reason is because this code plays with strings which in my mind would be more performance costly than just dealing with numbers. -
Tyler V. about 2 yearsThe only reason this isn't the accepted answer is because it returns the number of milliseconds where as the question is about unix timestamps, which are measured in seconds. So, you'll still have to do all of the division and Math.floor() that other answers suggest.