12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /**
- * 日期时间处理工具函数
- */
- // 格式化日期
- export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
- const d = date ? new Date(date) : new Date();
- const year = d.getFullYear();
- const month = String(d.getMonth() + 1).padStart(2, '0');
- const day = String(d.getDate()).padStart(2, '0');
- const hours = String(d.getHours()).padStart(2, '0');
- const minutes = String(d.getMinutes()).padStart(2, '0');
- const seconds = String(d.getSeconds()).padStart(2, '0');
- return format
- .replace('YYYY', year)
- .replace('MM', month)
- .replace('DD', day)
- .replace('HH', hours)
- .replace('mm', minutes)
- .replace('ss', seconds);
- }
- // 获取当前时间戳
- export function getCurrentTimestamp() {
- return Date.now();
- }
- // 获取指定日期的开始时间
- export function getStartOfDay(date) {
- const d = new Date(date);
- d.setHours(0, 0, 0, 0);
- return d;
- }
- // 获取指定日期的结束时间
- export function getEndOfDay(date) {
- const d = new Date(date);
- d.setHours(23, 59, 59, 999);
- return d;
- }
- // 计算两个日期之间的天数差
- export function getDaysDiff(date1, date2) {
- const d1 = new Date(date1);
- const d2 = new Date(date2);
- const diffTime = Math.abs(d2 - d1);
- return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
- }
- // 判断是否为同一天
- export function isSameDay(date1, date2) {
- const d1 = new Date(date1);
- const d2 = new Date(date2);
- return (
- d1.getFullYear() === d2.getFullYear() &&
- d1.getMonth() === d2.getMonth() &&
- d1.getDate() === d2.getDate()
- );
- }
|