libs/tools.js stringToDate dateToString 日期字符串转换函数
import { stringToDate } from '@/libs/tools.js'
export const stringToDate = (dateStr, separator) => {
if (!separator) {
separator = '-'
}
let dateArr = dateStr.split(separator)
let year = parseInt(dateArr[])
let month
// 处理月份为 04 这样的情况
if (dateArr[].indexOf('') === ) {
month = parseInt(dateArr[].substring())
} else {
month = parseInt(dateArr[])
}
let day = parseInt(dateArr[])
let date = new Date(year, month - , day)
return date
} export const dateToString = (date, separator) => {
if (!separator) {
separator = '-'
}
let year = date.getFullYear()
let month = (date.getMonth() + ).toString()
let day = (date.getDate()).toString()
if (month.length === ) {
month = '' + month
}
if (day.length === ) {
day = '' + day
}
var dateTime = year + separator + month + separator + day
return dateTime
}