new date()标准时间转yyyy-mm-dd hh:mm 24小时制

<!DOCTYPE html>
<html>
<head>
    <title>new date()标准时间转yyyy-mm-dd hh:mm 24小时制</title>
    <meta charset="utf-8" />
    <meta name="author" content="xusong" />
</head>
<body>
 
    <script type="text/javascript">
 
        // 扩充js的内置对象Date方法
        Object.assign(Date.prototype, {
            switch(time) {
                let date = {  
                     "yy": this.getFullYear(),
                     // 这里月份的key采用大写,为了区别分钟的key
                  "MM": this.getMonth() + 1,  
                  "dd": this.getDate(),  
                  "hh": this.getHours(),  
                  "mm": this.getMinutes(),  
                  "ss": this.getSeconds()
                };  
                //输出年 y+:匹配1个到多个y,i:忽略大小写
                if (/(y+)/i.test(time)) {  
                    time = time.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));  
                }  
                //输出月、日、时、分、秒
                Object.keys(date).forEach(function(i){
                    //  "(" + i + ")"的结果是字符串"(i+)",
                       // 只有写成"(" + i + ")"形式,才能在正则表达式中捕获子匹配,进而才能用到RegExp.$1的值
                    if (new RegExp("(" + i + ")").test(time)) { 
                          // 判断,如果时间为一位数,则在前面加'0' 
                          // ps:这里有一个小知识点:number类型+string类型 = string类型
                          if(RegExp.$1.length == 2){
                              date[i] < 10 ? date[i] = '0' + date[i]: date[i];
                          }
                          //替换初始化函数时候传入yyyy-mm-dd hh:mm:ss(这里可以打印出time、RegExp.$1、date[k])
                          time = time.replace(RegExp.$1, date[i]); 
                    }  
                })
                return time;  
            }
        })
 
        let newDate = new Date();
        // newDate.switch()传参的大小写要和方法内定义的key匹配
        document.write(newDate.switch('yyyy-MM-dd hh:mm:ss'));  
        //结果为:2017-03-24 20:24:36
        document.write(newDate.switch('yyyy-MM-dd'));  
        //结果为:2017-03-24
 
    </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/techliang666/p/9334020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值