在js中对时间类型格式化字符串
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; }
效果
代码段
我写的文章,除了纯代码,其他的都是想表达一种思想,一种解决方案.希望各位看官不要局限于文章中的现成的代码,要多关注整个文章的主题思路,谢谢!
我发布的代码,没有任何版权,遵守WTFPL协议(如有引用,请遵守被引用代码的协议)
qq群:5946699 希望各位喜爱C#的朋友可以在这里交流学习,分享编程的心得和快乐
我发布的代码,没有任何版权,遵守WTFPL协议(如有引用,请遵守被引用代码的协议)
qq群:5946699 希望各位喜爱C#的朋友可以在这里交流学习,分享编程的心得和快乐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述