dateFormater:格式化时间

 

function dateFormater(formater, t){    
  let date = t ? new Date(t) : new Date(),

    Y = date.getFullYear() + '',

    M = date.getMonth() + 1,

    D = date.getDate(),

    H = date.getHours(),

    m = date.getMinutes(),

    s = date.getSeconds();

  return formater.replace(/YYYY|yyyy/g,Y)

    .replace(/YY|yy/g,Y.substr(2,2))

    .replace(/MM/g,(M<10?'0':'') + M)

    .replace(/DD/g,(D<10?'0':'') + D)

    .replace(/HH|hh/g,(H<10?'0':'') + H)

    .replace(/mm/g,(m<10?'0':'') + m)

    .replace(/ss/g,(s<10?'0':'') + s)
}
// dateFormater('YYYY-MM-DD HH:mm', t) ==> 2019-06-26 18:30
// dateFormater('YYYYMMDDHHmm', t) ==> 201906261830
posted @ 2020-01-19 15:20  入坑的H  阅读(406)  评论(0编辑  收藏  举报