当前位置: 首页 > Web前端 > JavaScript

以给定时间格式输出指定时间

时间:2023-03-27 18:00:36 JavaScript

题目介绍:描述:\根据给定的时间格式输出指定的时间\格式说明\For2014.09.0513:14:20\yyyy:year,2014\yy:year,14\MM:month,两位数补齐,09\M:月,9\dd:日期,填两位,05\d:日期,5\HH:24小时,填两位,13\H:24小时,13\hh:12小时,填两位数,01\h:12小时,1\mm:分,填两位,14\m:分,14\ss:秒,填两位,20\s:秒,20\w:week,['day','one','two','three','four','five','six']之一,这个演示的结果是五个例子:\formatDate(newDate(1409894060000),'yyyy-MM-ddHH:mm:ssweekw')分析:\我自己的实现太片面了,这里换个综合解决方案。functionformatDate(date,format){functionadd0(time){返回时间<10?'0'+time:time}letweeks=['day','one','two','three','four','five','six'];让formatList={yyyy:date.getFullYear(),yy:date.getFullYear()%100,MM:add0(date.getMonth()+1),M:date.getMonth()+1,dd:add0(date.getDate()),d:date.getDate(),HH:add0(date.getHours()),H:date.getHours(),hh:add0(date.getHours()%12),h:date。getHours()%12,mm:add0(date.getMinutes()),m:date.getMinutes(),ss:add0(date.getSeconds()),s:date.getSeconds(),w:weeks[date.getDay()],}for(letkeyinformatList){format=format.replace(key,formatList[key])}返回format;}Summary:\上面的实现方法非常清晰易懂,实现方法也不麻烦,值得记录和学习