微信小程序new Date()转换时间异常问题

等级 103 0 0

微信小程序苹果手机页面上显示时间异常,安卓机正常问题 image 错误代码

var date = '2021-03-06 17:00:00'
var dateTime = new Date(date)

正确代码

var date = '2021-03-06 17:00:00'
var dateTime = new Date(date.replace(/-/g,'/'))

时间戳转换字符串方法

const 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()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function isBlank(str){
  if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空
    return true
  } else if (
    Object.prototype.toString.call(str) === '[object String]' || 
    Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
    return str.length==0?true:false
  } else if (Object.prototype.toString.call(str) === '[object Object]') {
    return JSON.stringify(str)=='{}'?true:false
  }else{
    return true
  }
}

module.exports = {
  formatTime: formatTime,
  isBlank: isBlank
}
预览图
收藏
评论区