import moment from 'moment';
// 本周日期
util.currentWeek = function() {
const time = {};
// 本周第一天
const startTime = new Date(new Date().getTime() - 3600 * 1000 * 24 * (new Date().getDay() - 1));
time.dateForm = moment(startTime).format('YYYY-MM-DD') + ' 00:00:00';
// 本周最后一天
const endTime = new Date(new Date().getTime() + 3600 * 1000 * 24 * (7 - new Date().getDay()));
time.dateTo = moment(endTime).format('YYYY-MM-DD') + ' 23:59:59';
return time;
};
// 本月日期
util.currentMonth = function() {
const time = {};
const date = new Date();
const start = date.setDate(1); // 本月第一天
time.dateForm = moment(start).format('YYYY-MM-DD') + ' 00:00:00';
const end = new Date(date.getFullYear(), date.getMonth() + 1, 0); // 本月的最后一天
time.dateTo = moment(end).format('YYYY-MM-DD') + ' 23:59:59';
return time;
};
// 本季度日期
util.currentQuarter = function() {
const time = {};
const date = new Date();
const month = date.getMonth();
const quarter = Math.floor(month / 3) + 1; // 从1开始,到4
const startMonth = (quarter * 3) - 2;
const lastMonth = quarter * 3;
console.log(startMonth, lastMonth);
date.setMonth(startMonth - 1);
const startDay = date.setDate(1);
time.dateForm = moment(startDay).format('YYYY-MM-DD') + ' 00:00:00';
date.setMonth(lastMonth);
const lastDay = date.setDate(0);
time.dateTo = moment(lastDay).format('YYYY-MM-DD') + ' 23:59:59';
return time;
};
// 本年日期
util.currentYear = function() {
const time = {};
let firstDay = new Date();
firstDay.setDate(1);
firstDay.setMonth(0);
let lastDay = new Date();
lastDay.setFullYear(lastDay.getFullYear() + 1);
lastDay.setDate(0);
lastDay.setMonth(-1);
time.dateForm = moment(firstDay).format('YYYY-MM-DD') + ' 00:00:00';
time.dateTo = moment(lastDay).format('YYYY-MM-DD') + ' 23:59:59';
return time;
};
export default util;
javascript 获取本周、本月、本季度、本年第一天,最后一天的日期
最新推荐文章于 2025-03-21 16:04:36 发布