返回时间日期的方法

 1 /**
 2   fmt 是预返回的时间的格式。例如: "yyyy-MM-dd hh:mm:ss q"==> 2019-12-17 14:15:16 4
 3                     "yy年MM月dd日 第q季度" ==> 2018年1月1日 第1季度 
 4   date Date类型
 5 */
 6 const dateTime = (fmt, date) => { 
 7     let o = {
 8       "M+": date.getMonth() + 1,    
 9       "d+": date.getDate(),   
10       "h+": date.getHours(),   
11       "m+": date.getMinutes(),   
12       "s+": date.getSeconds(),  
13       "q+": Math.floor((date.getMonth() + 3) / 3),  
14       "S": date.getMilliseconds()  
15     };
16     if (/(y+)/.test(fmt))
17       fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
18     for (let k in o)
19       if (new RegExp("(" + k + ")").test(fmt))
20         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
21     return fmt;
22   }

猜你喜欢

转载自www.cnblogs.com/ordinary-yolanda/p/12054486.html