JS——时间戳、new Date()与dayjs
·
蒽,没错,再次记录一下
// 时间戳转换成年月日时分秒
timeFormat(timestamp){
var time = new Date(timestamp);
var year = time.getFullYear();
var month = time.getMonth()+1;
var date = time.getDate();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
return year + '-' + this.addZero(month) + '-' + this.addZero(date) + ' ' + this.addZero(hours) + ':' + this.addZero(minutes) + ':' + this.addZero(seconds)
},
addZero(num) { return num < 10 ? '0' + num : num },
// 获取年月日时分秒
const getNowTime = () => {
const now = new Date();
const year = now.getFullYear(); // 获取四位数的年份
const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取月份(注意要加1,且补零)
const day = String(now.getDate()).padStart(2, '0'); // 获取日期(补零)
const hours = String(now.getHours()).padStart(2, '0'); // 获取小时(补零)
const minutes = String(now.getMinutes()).padStart(2, '0'); // 获取分钟(补零)
const seconds = String(now.getSeconds()).padStart(2, '0'); // 获取秒(补零)
const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return formattedDateTime
}
// dayjs
dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
// getStartOfMonth
dayjs().startOf('month').format('YYYY-MM-DD')
// getEndOfMonth
dayjs().endOf('month').format('YYYY-MM-DD')
// getNowMonth
dayjs().endOf('month').format('YYYY-MM')
export function getStartOfMonth() {
return dayjs().startOf('month').format('YYYY-MM-DD');
}
export function getEndOfMonth() {
return dayjs().endOf('month').format('YYYY-MM-DD');
}
export function getNowMonth() {
return dayjs().endOf('month').format('YYYY-MM');
}
export function splitDate(dateString) {
return dateString.split('-');
}
更多推荐



所有评论(0)