格式化日期

// 时间格式化 timestamp:时间戳 fmt:'yyyy-MM-dd hh:mm:ss'
function formatDate(timestamp, fmt) {
     if (!timestamp) return '';
     let date = new Date(timestamp);
     let o = {
         'y+': date.getFullYear(),
         'M+': date.getMonth() + 1,
         'd+': date.getDate(),
         'h+': date.getHours(),
         'm+': date.getMinutes(),
         's+': date.getSeconds()
     };
     for (let k in o) {
         if (new RegExp('(' + k + ')').test(fmt)) {
             let str = o[k] + '';
             fmt = fmt.replace(RegExp.$1, (str.length === 1) ? ('0' + str) : str);
         }
     };
     return fmt;
 }
 

 

posted @ 2020-08-11 17:38  江离白芷  阅读(112)  评论(0)    收藏  举报