时间转换为时间戳(此项目是考试开始时间到页面自动刷新)

//把日期转换成时间戳
function get_unix_time(time1){
    var newstr = time1.replace(/-/g,'/');
    var date =  new Date(newstr);
    var time_str = date.getTime().toString();
    return time_str.substr(0, 10);
}

// 获取出当前时间转换为'2016-05-26 15:43:00'格式

function currntData(){
    var obj = new Date();
    var y = obj.getFullYear();
    var m = obj.getMonth()+1;
    var d = obj.getDate();
    var h = obj.getHours();
    var i = obj.getMinutes();
    var s = obj.getSeconds();
    if(m<10)m="0"+m;
    if(d<10)d="0"+d;
    if(h<10)h="0"+h;
    if(i<10)i="0"+i;
    if(s<10)s="0"+s;
    return time = y+'-'+m+'-'+d+' '+h+':'+i+':'+s;
}

var currntData = currntData();
var startDate = '2016-05-26 15:43:00';

// 开始开始时间与用户进入的当前时间之差
var time = (get_unix_time(startDate) - get_unix_time(currntData));

if( time >0 ){
    var setDate = setInterval(function(){
                   time--;

       //考试时间到
                   if(time==0){
                       alert(25)
                       clearInterval(setDate);
                   }
              },1000)

};

另外 :timeNow= Date.parse(new Date());   Date.parse()方法也可以将当前时间转换成时间戳

原文地址:https://www.cnblogs.com/web-leader/p/5531667.html