js字符串时间格式转为Date格式
/**
* @module Date
*/
/**
* @example
* stringToDate()
* // returns the timestamp
*
*/
const stringToDate = (str) => {
let date = ''
if (!str) return date
const tempStrs = str.split(' ')
const dateStrs = tempStrs[0].split('-')
const year = parseInt(dateStrs[0], 10)
const month = parseInt(dateStrs[1], 10) - 1
const day = parseInt(dateStrs[2], 10)
const timeStrs = tempStrs[1].split(':')
const hour = parseInt(timeStrs[0], 10)
const minute = parseInt(timeStrs[1], 10)
const second = parseInt(timeStrs[2], 10)
date = new Date(year, month, day, hour, minute, second)
return date
}
此篇博客介绍了如何使用JavaScript将字符串格式的时间转换为Date对象,通过示例展示了具体步骤和函数实现,适合前端开发者快速掌握时间处理技巧。
1627

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



