1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt) { var o = { "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+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, ( this .getFullYear() + "" ).substr(4 - RegExp.$1.length)); for ( var k in o) if ( new RegExp( "(" + k + ")" ).test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (( "00" + o[k]).substr(( "" + o[k]).length))); return fmt; } 使用: var time1 = new Date().Format( "yyyy-MM-dd" ); var time2 = new Date().Format( "yyyy-MM-dd HH:mm:ss" ); |
1 2 3 4 | //字符串转化为日期格式 var data1 = "2022-07-27 12:01:16" ; var data2 = new Date(data1); |
分类:
JavaScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2010-07-27 图像的排列属性ALIGN