一、将时间转化为 年-月-日 格式 函数
/**
* @description: 将时间转化为 年-月-日 格式
* @param timestamp
* @return {String}
*/
getDateFormatted(timestamp) {
let date
if (timestamp) {
date = new Date(timestamp) // 将时间转换为 Date 对象
} else {
date = new Date() // 获取当前时间
}
const year = date.getFullYear() // 获取年份
const month = String(date.getMonth() + 1).padStart(2, '0') // 获取月份,并且补齐到两位数
const day = String(date.getDate()).padStart(2, '0') // 获取日期,并且补齐到两位数
const formattedDate = `${year}-${month}-${day}` // 拼接成 年-月-日 格式的字符串
return formattedDate
}
const _date = new Date().getTime() + 3600 * 1000 * 24 * 7 // 获取一周后的日期
this.getDateFormatted(_date ) // 得到一周后的 年-月-日 格式 日期
二、获取当前时间 "00:00:00"格式 函数
/**
* @description: 获取当前时间 "00:00:00"格式
* @return {String}
*/
getCurrentTime() {
const now = new Date() // 获取当前时间
const hours = now.getHours().toString().padStart(2, '0') // 获取小时,并且补齐到两位数
const minutes = now.getMinutes().toString().padStart(2, '0') // 获取分钟,并且补齐到两位数
const seconds = now.getSeconds().toString().padStart(2, '0') // 获取秒数,并且补齐到两位数
const currentTimeString = `${hours}:${minutes}:${seconds}` // 拼接成 时:分;秒 格式的字符串
return currentTimeString
},
this.getCurrentTime() // 取当前时间 "00:00:00"格式