移动端日期时间兼容性 之 “坑”

1、js时间函数getTime() 在苹果手机上返回NaN的问题
在苹果手机上时间格式使用“/” 隔开表示。

const start_time = '2019-10-21 00:00:00'
const start = (new Date(start_time.replace(/-/g, '/'))).getTime()     // 得到时间戳

2、获取当天时间的23:59:59的时间戳

getToday () {
   var myDate = new Date()
   myDate.getFullYear() // 获取完整的年份
   myDate.getMonth() // 获取当前月份(0-11,0代表1月)
   myDate.getDate() // 获取当前日(1-31)
   const today = myDate.getFullYear() + '/' + (myDate.getMonth() + 1) + '/' + myDate.getDate() + ' ' + '23:59:59'
   return new Date(today).getTime()
}

3、时间范围比较 (时间戳的比较)

const start = (new Date(item.Data.start_time.replace(/-/g, '/'))).getTime()
const end = (new Date(item.Data.end_time.replace(/-/g, '/'))).getTime() + 24 * 60 * 60 * 1000 - 1
const today = this.getToday()
if (today < start) { // 未开始
 this.recordInfo.dateStatu = 1
} else if (today > end) { // 已结束
 this.recordInfo.dateStatu = 3
} else { // 进行中
 this.recordInfo.dateStatu = 2
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容