用户输入一个开始时间,如2021-05-27,然后输入结束时间2022-1-31,那么判断在这个时间段内存在的所以季度并打印,如2021年5月为第二季度,那么就写成202102,2021代表对应年,02代表此年的第二个季度。
输入2021-05-27,2022-1-31开始时间和结束时间两个参数,然后打印出其中对应几个季度,结果如下 :202102-202103-202104-202201
封装第一个函数:功能,传入月份,输出季度
function jidu(num) {
var shu = 1
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
for (let i = 0; i < arr.length; i++) {
let arrtwo = arr[i]
for (let j = 0; j < arrtwo.length; j++) {
if (num == arrtwo[j]) {
return shu
}
}
var shu = shu + 1
}
}
//添加日期后,返回修改后的日期
function AddDay(startDate, addMonth, jiezhi) {
//格式化参数 2022-02-27T00:00:00.000Z
var date = new Date(startDate);
var datetwo = new Date(jiezhi);
// 计算addDay天后的日期 2022 1 32传入date将天数重置
var bu = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var newDate = new Date(date.getFullYear(), date.getMonth() + addMonth, date.getDate());
var newDatetwo = new Date(datetwo.getFullYear(), datetwo.getMonth(), datetwo.getDate());
// 增加超出后返回undefined
if (newDate > newDatetwo) {
// 如果开始时间和结束时间在不同季度,那么打印最后一个季度
if (jidu(bu.getMonth() + 1) !== jidu(newDatetwo.getMonth() + 1)) {
console.log('20220' + jidu(newDatetwo.getMonth() + 1) + '---');
}
return undefined
}
//计算修改后日期
var year2 = newDate.getFullYear(); // 2022
var month2 = newDate.getMonth() + 1; // 3
var day2 = newDate.getDate(); // 4
return year2 + "-" + month2 + "-" + day2
}
//计算日期之间的月数
function datemonth(startDate, endDate) {
startDate = new Date(startDate.replace(/-/g, '/'));
endDate = new Date(endDate.replace(/-/g, '/'));
var num = 0;
var year = endDate.getFullYear() - startDate.getFullYear();
num += year * 12;
var month = endDate.getMonth() - startDate.getMonth();
num += month;
var day = endDate.getDate() - startDate.getDate();
if (day > 0) {
num += 1;
} else if (day < 0) {
}
return num;
}
// 传入月,输出季度
function jidu(num) {
var shu = 1
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
for (let i = 0; i < arr.length; i++) {
let arrtwo = arr[i]
for (let j = 0; j < arrtwo.length; j++) {
if (num == arrtwo[j]) {
return shu
}
}
var shu = shu + 1
}
}
//添加日期后,返回修改后的日期
function AddDay(startDate, addMonth, jiezhi) {
//格式化参数 2022-02-27T00:00:00.000Z
var date = new Date(startDate);
var datetwo = new Date(jiezhi);
// 计算addDay天后的日期 2022 1 32传入date将天数重置
var bu = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var newDate = new Date(date.getFullYear(), date.getMonth() + addMonth, date.getDate());
var newDatetwo = new Date(datetwo.getFullYear(), datetwo.getMonth(), datetwo.getDate());
// 增加超出后返回undefined
if (newDate > newDatetwo) {
// 如果开始时间和结束时间在不同季度,那么打印最后一个季度
if (jidu(bu.getMonth() + 1) !== jidu(newDatetwo.getMonth() + 1)) {
console.log('20220' + jidu(newDatetwo.getMonth() + 1) + '---');
}
return undefined
}
//计算修改后日期
var year2 = newDate.getFullYear(); // 2022
var month2 = newDate.getMonth() + 1; // 3
var day2 = newDate.getDate(); // 4
return year2 + "-" + month2 + "-" + day2
}
//计算日期之间的月数
function datemonth(startDate, endDate) {
startDate = new Date(startDate.replace(/-/g, '/'));
endDate = new Date(endDate.replace(/-/g, '/'));
var num = 0;
var year = endDate.getFullYear() - startDate.getFullYear();
num += year * 12;
var month = endDate.getMonth() - startDate.getMonth();
num += month;
var day = endDate.getDate() - startDate.getDate();
if (day > 0) {
num += 1;
} else if (day < 0) {
}
return num;
}
var asd = "2021-05-27"
var jiezhi = "2022-1-31"
var chaMonth = datemonth(asd, jiezhi) //计算日期中间月数
if (chaMonth < 0) {
var dateee = new Date(asd);
var newDateee = new Date(dateee.getFullYear(), dateee.getMonth(), dateee.getDate());
var aaa = newDateee.getMonth() + 1
let wwww = jidu(aaa)
console.log("输入错误请重输");
} else {
// 获取第一个月季度
var dateee = new Date(asd);
var newDateee = new Date(dateee.getFullYear(), dateee.getMonth(), dateee.getDate());
var bbb = newDateee.getFullYear()
var aaa = newDateee.getMonth() + 1
let wwww = jidu(aaa)
console.log(bbb+'0' + wwww );
let xiuData = AddDay(asd, 3, jiezhi)
// 循环+3月然后输出对应季度
while (xiuData !== undefined) {
var d = new Date(xiuData);
var n = new Date(d.getFullYear(), d.getMonth(), d.getDate());
var a = n.getMonth() + 1
let xiuJD = jidu(a)
if(xiuJD){
}
console.log('20220' + xiuJD + '---');
xiuData = AddDay(xiuData, 3, jiezhi)
}
}