How to convert Regular Date to unixtime in Javascript?

我的梦境 提交于 2019-12-11 14:09:13

问题


How can i convert date which looks like this 12092008 to unixtime like this 1221215809


回答1:


You may want to use the following:

function covertToUnixTime(yourDate) {
    return new Date(yourDate.substring(4, 8) + '/' + 
                    yourDate.substring(2, 4) + '/' + 
                    yourDate.substring(0, 2)).getTime() / 1000;
}

covertToUnixTime('12092008');   // Returns: 1221170400



回答2:


You could use jQuery datepicker's utility functions, parseDate and formatDate.




回答3:


This worked for me.

var day = $("#day").val();
var month   = $("#month").val();
var year    = $("#year").val();
var date    = new Date();
date.setFullYear(year, month, day)
var unixtimeMS = date.getTime();
var unixtime = parseInt(unixtimeMS / 1000);
alert(unixtime);


来源:https://stackoverflow.com/questions/2295939/how-to-convert-regular-date-to-unixtime-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!