微信小程序 时间格式化处理方法【兼容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}`
}
微信公众号:jingfeng18 免费学习领取最新的前端学习资源
原文地址:https://www.cnblogs.com/qianduanshiping/p/11826839.html