/**
* 获取自然月开始结束时间
* @param {*} year
* @param {*} month
*/
function getMonthBeginEnd(year, month) {
if (~~year === 0 || ~~month === 0) {
throw new Error('年月参数错误');
}
const result = {begin: 0, end: 0};
const target = new Date();
target.setFullYear(year);
target.setMonth(month - 1);
target.setDate(1);
target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(0);
result.begin = target.getTime();
const now = new Date();
if (target.getFullYear() + target.getMonth() >= now.getFullYear() + now.getMonth()) {
result.end = now.getTime();
console.log(result);
console.log(new Date(result.begin), new Date(result.end));
return result;
}
target.setMonth(month);//当月的下个月1日 0时0分0秒
target.setSeconds(-1);//前1秒,即昨天最后1秒钟,也就是上个月的最后一秒钟
result.end = target.getTime();
console.log(result);
console.log(new Date(result.begin), new Date(result.end));
return result;
}
/**
* 获取自然日开始结束时间
* @param {*} year
* @param {*} month
* @param {*} date
*/
function getDateBeginEnd(year, month, date) {
if (~~year === 0 || ~~month === 0 || ~~date === 0) {
throw new Error('年月日参数错误');
}
const result = {begin: 0, end: 0};
const target = new Date();
target.setFullYear(year);
target.setMonth(month - 1);
target.setDate(date);
target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(0);
result.begin = target.getTime();
const targetDateCount = target.getFullYear() + target.getMonth() + target.getDate();
const now = new Date();
const nowDateCount = now.getFullYear() + now.getMonth() + now.getDate();
if (targetDateCount >= nowDateCount) {
result.end = now.getTime();
console.log(0, result);
console.log(new Date(result.begin), new Date(result.end));
return result;
}
target.setDate(target.getDate() + 1);//次日0时0分0秒
target.setSeconds(-1);//前1秒,即当日最后1秒钟
result.end = target.getTime();
console.log(1, result);
console.log(new Date(result.begin), new Date(result.end));
return result;
}
/**
* 获取自然周开始结束时间
* @param {*} year
* @param {*} month
* @param {*} number 第几周
*/
function getWeekByMonth(year, month, number) {
const weekCount = getWeekCountInMonth(year, month);
const result = {begin: 0, end: 0, list: weekCount};
const target = new Date();
target.setFullYear(year);
target.setMonth(month - 1);
target.setDate(1);
target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(0);
let targetDay = target.getDay();
targetDay = targetDay === 0 ? 7 : targetDay;
if (number === weekCount[0]) {
console.log(month + '月第' + number + '周起始', target);
result.begin = target.getTime();
target.setDate(target.getDate() + (7 - targetDay));//设置到周日最后一秒
target.setHours(23);
target.setMinutes(59);
target.setSeconds(59);
target.setMilliseconds(0);
console.log(month + '月第' + number + '周截止', target);
result.end = target.getTime();
console.log(result);
return result;
}
target.setDate(target.getDate() + (7 - targetDay) + 1);//对其到周一,设置到第二个周一0时0分0秒
target.setDate(target.getDate() + 7 * (number - 2));//设置到第n周周日
console.log(month + '月第' + number + '周起始', target);
result.begin = target.getTime();
if (number === weekCount[weekCount.length - 1]) {
target.setMonth(month);//次月1日
target.setDate(1);
target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(-1);//向前到当月最后1毫秒
result.end = target.getTime();
console.log(month + '月第' + number + '周截止', target);
console.log(result);
return result;
}
target.setDate(target.getDate() + 6);//设置到第n周周日
target.setHours(23);
target.setMinutes(59);
target.setSeconds(59);
target.setMilliseconds(0);
result.end = target.getTime();
console.log(month + '月第' + number + '周截止', target);
console.log(result);
return result;
}
/**
* 获取每月有多少周
* @param {*} year
* @param {*} month
*/
function getWeekCountInMonth(year, month) {
const list = [];
const target = new Date();
let offset = 1;
target.setFullYear(year);
target.setMonth(month - 1);
target.setDate(1);
target.setHours(0);
target.setMinutes(0);
target.setSeconds(0);
target.setMilliseconds(0);
let targetDay = target.getDay();
targetDay = targetDay === 0 ? 7 : targetDay;
if (targetDay !== 1) {
console.log(targetDay);
list.push(offset);
offset = offset + 1;
target.setDate(target.getDate() + (7 - targetDay) + 1);//对齐到周一
}
for (let i = offset; i <= 5; i++) {
list.push(offset);
offset = offset + 1;
target.setDate(target.getDate() + 7);//设置到次周一0时分秒
target.setMilliseconds(-1);//向前1毫秒到本周最后1毫秒
console.log(target);
if ((target.getMonth() + 1) !== month) {
break;
}
}
console.log(month + '月周数', list);
return list;
}
/**
* 获取上一月时间
* @param {} date
*/
function getPreMonth (date) {
var _date = dateFormat('YYYY-MM', new Date(date));
var arr = _date.split('-');
var year = arr[0]; // 获取当前日期的年份
var month = arr[1]; // 获取当前日期的月份
var day = arr[2]; // 获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); // 获取当前日期中月的天数
var year2 = year;
var month2 = parseInt(month) - 1;
if (month2 === 0) {
year2 = parseInt(year2) - 1;
month2 = 12;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;
}
var t2 = year2 + '-' + month2;
return t2;
}
/**
* 获取下一月时间
* @param {时间} date
*/
function getNextMonth (date) {
var _date = dateFormat('YYYY-MM', new Date(date));
var arr = _date.split('-');
var year = arr[0]; // 获取当前日期的年份
var month = arr[1]; // 获取当前日期的月份
var day = arr[2]; // 获取当前日期的日
var days = new Date(year, month, 0);
days = days.getDate(); // 获取当前日期中的月的天数
var year2 = year;
var month2 = parseInt(month) + 1;
if (month2 === 13) {
year2 = parseInt(year2) + 1;
month2 = 1;
}
var day2 = day;
var days2 = new Date(year2, month2, 0);
days2 = days2.getDate();
if (day2 > days2) {
day2 = days2;
}
if (month2 < 10) {
month2 = '0' + month2;
}
var t2 = year2 + '-' + month2;
return t2;
}
/**
* 格式化时间
* @param {YYYY-MM-DD hh:mm:ss} String
* @param {时间} date
*/
function dateFormat (fmt, date) {
date = new Date(date);
let ret;
let opt = {
'Y+': date.getFullYear().toString(), // 年
'M+': (date.getMonth() + 1).toString(), // 月
'D+': date.getDate().toString(), // 日
'h+': date.getHours().toString(), // 时
'm+': date.getMinutes().toString(), // 分
's+': date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (let k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
}
}
return fmt;
}
// getMonthBeginEnd(2019, 9);
// getDateBeginEnd(2019, 10, 16);
// getWeekByMonth(2019, 10, 5);
原文地址:https://www.cnblogs.com/Lewiskycc/p/11697586.html
时间: 2024-10-30 13:35:46