背景:
使用el-date-picker组件进行时间选择,展示为yyyy-MM-dd格式,但是需要给后台传的数据格式为yyyy-MM-dd HH:mm:ss:000,所以需要对日期格式进行处理,后端索要数据为结束日期的下一天的00时00分00秒
所以我所用的方法是:
console.log('this.ruleForm.date[1]', this.ruleForm.date[1])
//this.ruleForm.date[1] 2021-4-19
let end_time =
new Date(this.ruleForm.date[1])
end_time.setTime(end_time.getTime() + 3600 * 1000 * 24 * 1)
let end =
end_time.getFullYear() +
'-' +
(end_time.getMonth() + 1) +
'-' +
end_time.getDate() + ' 00:00:00.000'
safari浏览器报错,原因为参数错误
出现原因:
执行完let end_time = new Date(this.ruleForm.date[1])步骤后:
谷歌输出的是:Tue Apr 20 2021 00:00:00 GMT+0800 (中国标准时间)
而safari输出的结果为:Invalid Date
在js开发中也经常会把yyyy-MM-dd HH:mm:ss格式字符串格式化为javascript Date类型,如new Date(‘2018-11-11 00:00:00’),但是此操作在Safari浏览器上会报错
解决方法:
把字符串转化为Safari支持的格式
yyyy/MM/dd HH:mm:ss或yyyy-MM-ddTHH:mm:ss或其他
// let end_time = new Date(this.ruleForm.date[1])
let end_time = new Date(this.ruleForm.date[1].replace(/-/g, '/'))
问题解决~~