码迷,mamicode.com
首页 > 其他好文 > 详细

时间转换模板

时间:2018-01-12 00:04:25      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:记录   格式转换   col   seconds   get   cti   date   span   reg   

最近开发手机界面时,经常用到时间格式转换,所以觉得有必要记录下来;代码如下:

//时间转换模板
        Date.prototype.format = function(format) {
            var date = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S+": this.getMilliseconds()
                };
            if (/(y+)/i.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1
                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                }
            }
            return format;
        }

如有问题,请指出来,谢谢!

时间转换模板

标签:记录   格式转换   col   seconds   get   cti   date   span   reg   

原文地址:https://www.cnblogs.com/lidelin/p/8270781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!