// 将Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)转化为yyyy-MM-dd
parseTime (str) {
if ((str + '').indexOf('-') != -1) {
str = str.replace(new RegExp(/-/gm), '/')
}
let d = new Date(str)
let newDateYear = d.getFullYear()
let newDateMonth = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)
let newDateDay = d.getDate() + '' < 10 ? '0' + d.getDate() + '' : d.getDate() + ''
return newDateYear + '-' + newDateMonth + '-' + newDateDay
},
本文介绍了一种将特定格式的日期字符串转换为'yyyy-MM-dd'格式的方法。通过使用正则表达式替换日期分隔符,并利用JavaScript的Date对象解析原始日期,最终实现了日期格式的标准化。这一技巧对于处理和统一不同来源的日期数据非常实用。
2万+

被折叠的 条评论
为什么被折叠?



