new Date() 倒计时功能

js中单独调用new Date() 显示的结果是:Fri May 20 2015 20:00:00 GMT+0800这种格式的时间

JS获取当前时间戳的方法

JavaScript 获取当前时间戳:
第一种方法:

var time =Date.parse(new Date());
结果:1463732230000
第二种方法:

var time =(new Date()).valueOf();
结果:1463732231455

第三种方法:

var time=new Date().getTime();
结果:1463732299085

    var ems = document.getElementsByTagName("em");
    var t_time = "2016/05/20 20:00:00";    
    s_time = new Date(t_time);
    s_time = Date.parse(t_time) / 1000;   
   (function setTime(){
        var n_time = Date.parse(new Date()) /1000;
        var time = s_time-n_time;
        if (time>0) {
            var d = parseInt(time / 86400);
            time = time - d * 86400;
            var h = parseInt(time / 3600);
            time = time - h * 3600;
            var m = parseInt(time / 60);
            time = time - m * 60;
            var s = time;
            ems[3].innerHTML = s;
            ems[2].innerHTML = m;
            ems[1].innerHTML = h;
            ems[0].innerHTML = d;
            setTimeout(setTime, 500);
        }else {
            alert('倒计时结束了')
        }
    })();

posted @ 2016-05-22 23:41  liangliang74  阅读(330)  评论(0编辑  收藏  举报