javascript 获取本周、本月、本季度、本年第一天,最后一天的日期

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;
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值