evo-Financial-vue/src/plugins/DateUtils.js
2025-09-19 15:56:35 +08:00

57 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default{
getCurrentMonthStartDates(currentDate) {
const firstDay = new Date(currentDate);
firstDay.setDate(1);
// 格式化日期为YYYY-MM-DD格式
return firstDay.toISOString().slice(0, 10);
},
getCurrentMonthEndDates(currentDate) {
const lastDay = new Date(currentDate);
lastDay.setMonth(lastDay.getMonth() + 1, 0); // 设置日期为下个月的第一天的前一天,即当前月的最后一天
// 格式化日期为YYYY-MM-DD格式
return lastDay.toISOString().slice(0, 10);
},
formatDate(date, pattern){
// 如果是时间戳,先转换为 Date 对象
if (typeof date === 'number') {
date = new Date(date);
}
// 如果是字符串,尝试转换为 Date 对象
if (typeof date === 'string') {
date = new Date(date.replace(/-/g, '/')); // 处理 Safari 兼容性
}
const options = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false // 24小时制
};
// 简单格式直接返回
if (pattern === 'yyyy-MM-dd HH:mm:ss') {
return date.toLocaleString('zh-CN', options).replace(/\//g, '-');
}
// 自定义格式解析(示例支持 yyyy、MM、dd、HH、mm、ss
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hour = String(date.getHours()).padStart(2, '0');
const minute = String(date.getMinutes()).padStart(2, '0');
const second = String(date.getSeconds()).padStart(2, '0');
return pattern.replace('yyyy', year)
.replace('MM', month)
.replace('dd', day)
.replace('HH', hour)
.replace('mm', minute)
.replace('ss', second);
}
}