博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS 时间转换为时间戳

Posted on 2018-01-11 21:08  哈哈丶大傻瓜  阅读(284)  评论(0编辑  收藏  举报
 1 Date.prototype.format = function(fmt) {
 2     var o = {
 3         "M+" : this.getMonth()+1,                 //月份
 4         "d+" : this.getDate(),                    //
 5         "h+" : this.getHours(),                   //小时
 6         "m+" : this.getMinutes(),                 //
 7         "s+" : this.getSeconds(),                 //秒        "q+" : Math.floor((this.getMonth()+3)/3), //季度
 8         "S"  : this.getMilliseconds()             //毫秒
 9     };
10     if(/(y+)/.test(fmt)) {
11         fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
12     }
13     for(var k in o) {
14         if(new RegExp("("+ k +")").test(fmt)){
15             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
16         }
17     }
18     return fmt;
19 }
temp = '2016-8-10 10:00:00'
date_1 = new Date(new Date(temp).getTime()).format("MM-dd hh:mm:ss"); # 将年份去掉了