js修改时间格式函数

export function getNowFormatDate(data) {
  var date = new Date(data)
  var seperator1 = '/'
  var seperator2 = ':'
  var month = date.getMonth() + 1

  var strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }

  var currentdate = date.getFullYear() + seperator1 + prefixZero(month, 2) + seperator1 + prefixZero(strDate, 2) +
          ' ' + prefixZero(date.getHours(), 2) + seperator2 + prefixZero(date.getMinutes(), 2) +
          seperator2 + prefixZero(date.getSeconds(), 2)
  return currentdate
}

function prefixZero(num, n) {
  return (Array(n).join(0) + num).slice(-n)
}

  

posted @ 2020-09-03 18:05  石shi  阅读(398)  评论(0编辑  收藏  举报