Convert normal date to unix timestamp

304,702

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

Share:
304,702

Related videos on Youtube

Stan
Author by

Stan

Updated on July 15, 2022

Comments

  • Stan
    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
      Martin York almost 12 years
    • Gareth Parker
      Gareth Parker almost 12 years
      Have you tried newDate.getTime() / 1000?
    • Stan
      Stan almost 12 years
      @LokiAstari yes, sorry. I did search but didn't find that.
    • Michal Stefanow
      Michal Stefanow about 5 years
      Maybe 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 using Math.floor((+new Date()) / 1000);
    • Ank_247shbm
      Ank_247shbm about 2 years
  • B T
    B T almost 10 years
    Math.floor it or its not a unix timestamp (it'll have a decimal)
  • WP Learner
    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
    theVinchi over 8 years
    @user2584538, see my answer below. You must remove the decimals.
  • Capaj
    Capaj almost 8 years
    just beware that this return a string, not a number.
  • theVinchi
    theVinchi over 7 years
    Good point, changed answer to wrap with parseInt() to convert back to integer.
  • WeizhongTu
    WeizhongTu over 7 years
    Why does Date.parse give incorrect results?: stackoverflow.com/a/2587398/2714931
  • Capricorn
    Capricorn almost 6 years
    How does this answer differentiate from the one given by Barmar 6 years ago?
  • Mentor
    Mentor about 5 years
    Did something change since this answer? .getTime() returns UNIX timestamps at the time of writing this...
  • Laurens
    Laurens almost 5 years
    Just 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
    Nicu Surdu almost 5 years
    @Mentor UNIX time is measured in seconds, not milliseconds
  • Shahin Ghasemi
    Shahin Ghasemi about 4 years
    why did you divide the result to 1000? because getTime will give you in ms
  • mindplay.dk
    mindplay.dk almost 3 years
    If performance matters, probably the better and more correct option is Math.round or Math.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
    kasvith about 2 years
    should be the accepted answer
  • jaquinocode
    jaquinocode about 2 years
    I 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.
    Tyler V. about 2 years
    The 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.