JS DATE对象详解

1、建立时间对象:可获取年,月,日,星期,时,分,秒

var d = new Date();
console.log(d.getFullYear()+'年'+d.getMonth()+'月'+d.getDate()+'星期'+d.getDay()+'日'+d.getHours()+'小时'+d.getMinutes()+'分'+d.getSeconds()+'秒');

 2、获取时间戳

console.log(d.getTime());

 3、时间转换公式

	Math.floor(t/86400) //天
	Math.floor(t%86400/3600) //时
	Math.floor(t%86400%3600/60) //分
	t%60 //秒

 4、js格式化日期

var format = function(time, format){
    var t = new Date(time);
    var tf = function(i){return (i < 10 ? '0' : '') + i};
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
    switch(a){
        case 'yyyy':
        return tf(t.getFullYear());
        break;
        case 'MM':
        return tf(t.getMonth() + 1);
        break;
        case 'mm':
        return tf(t.getMinutes());
        break;
        case 'dd':
        return tf(t.getDate());
        break;
        case 'HH':
        return tf(t.getHours());
        break;
        case 'ss':
        return tf(t.getSeconds());
        break;
    }
    })
}
format(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss')//getTime返回距 1970 年 1 月 1 日之间的毫秒数

 

posted @ 2015-07-08 16:25  我爱小明  阅读(517)  评论(0编辑  收藏  举报