微信小程序 時間格式化處理方法【兼容ios】

ios不能識別日期格式爲 2019-11-06 20:00:01 或者  2019.06.21 20:00:01 中的 - 或者 . ,

所以使用new Date("2019-06-21 20:00:01")不能將字符串轉換爲Date,需要將 -或者. 轉換成 / 即可解決問題。

let _date = endTime.replace(/\.|\-/g, '/');

let endDate = new Date(_date );
 

時間處理方法

export function dateFormat (fmt, date) {
  let ret
  let opt = {
    'Y+': date.getFullYear().toString(),
    'm+': (date.getMonth() + 1).toString(),
    'd+': date.getDate().toString(),
    'H+': date.getHours().toString(),
    'M+': date.getMinutes().toString(),
    'S+': date.getSeconds().toString()

  }
  for (let k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
    }
  }
  return fmt
}

 

微信自帶的處理方法

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章