how to convert a string to a Unix timestamp in javascript?

后端 未结 4 1519
一生所求
一生所求 2020-12-01 12:14

I want to convert a string \"2013-09-05 15:34:00\" into a Unix timestamp in javascript. Can any one tell how to do that? thanks.

4条回答
  •  情深已故
    2020-12-01 12:27

    You can initialise a Date object and call getTime() to get it in unix form. It comes out in milliseconds so you'll need to divide by 1000 to get it in seconds.

    (new Date("2013/09/05 15:34:00").getTime()/1000)
    

    It may have decimal bits so wrapping it in Math.round would clean that.

    Math.round(new Date("2013/09/05 15:34:00").getTime()/1000)
    

提交回复
热议问题