博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在js中对时间类型格式化字符串
阅读量:7155 次
发布时间:2019-06-29

本文共 1696 字,大约阅读时间需要 5 分钟。

Date.prototype.toString = function (format) {    if (format == null) {        format = "yyyy-MM-dd HH:mm:ss";    }    format = format.replace(/yyyy/g, this.getFullYear());    format = format.replace(/yyy/g, this.getYear());    format = format.replace(/yy/g, this.getFullYear().toString().slice(-2));    if (format.indexOf('mi') >= 0) {        format = format.replace(/mi/g, this.getMilliseconds().toString());    }    if (format.indexOf('M') >= 0) {        var M = (this.getMonth() + 1).toString();        format = format.replace(/MM/g, ("0" + M).slice(-2));        format = format.replace(/M/g, M);    }    if (format.indexOf('ddd') >= 0) {        var xq = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];        format = format.replace(/ddd/g, xq[this.getDay()]);    }    if (format.indexOf('d') >= 0) {        var d = this.getDate().toString();        format = format.replace(/dd/g, ("0" + d).slice(-2));        format = format.replace(/d/g, d);    }    if (format.indexOf('h') >= 0 || format.indexOf('H') >= 0) {        var h = this.getHours();        format = format.replace(/HH/g, ("0" + h.toString()).slice(-2));        format = format.replace(/H/g, h);        h = h % 12;        format = format.replace(/hh/g, ("0" + h.toString()).slice(-2));        format = format.replace(/h/g, h);    }    if (format.indexOf('m') >= 0) {        var m = this.getMinutes().toString();        format = format.replace(/mm/g, ("0" + m).slice(-2));        format = format.replace(/m/g, m);    }    if (format.indexOf('s') >= 0) {        var s = this.getSeconds().toString();        format = format.replace(/ss/g, ("0" + s).slice(-2));        format = format.replace(/s/g, m);    }    return format;}

效果

代码段

转载地址:http://hqhgl.baihongyu.com/

你可能感兴趣的文章
数据处理中白化Whitening的作用图解分析
查看>>
HDFS API编程
查看>>
【CF】328 D. Super M
查看>>
Nodejs Guides(二)
查看>>
EL表达式
查看>>
本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。...
查看>>
调用系统拍照
查看>>
NSUserDefaults保存应用中的数据
查看>>
用Gvim建立IDE编程环境 (Windows篇)_Nothing is impossible for a willing heart._百度空间...
查看>>
Chrome 插件
查看>>
c++的重载,覆盖与隐藏
查看>>
大数据的三个入口
查看>>
void指针
查看>>
hackerrank-knapsack
查看>>
Atitit. 软件开发中的管理哲学--一个伟大的事业必然是过程导向为主 过程导向 vs 结果导向...
查看>>
hive load from hdfs出错
查看>>
IOS开发:xcode5版本引发的问题
查看>>
asp.net 负载均衡下session存储的解决方法
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序
查看>>
领域驱动开发推荐代码示例 — Microsoft NLayerApp
查看>>