当前位置:网站首页>js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
2022-06-25 22:08:00 【渊来有你】
问题描述:
用户输入一个开始时间,如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)
}
}
结果如下:
边栏推荐
- C# IO Stream 流(一)基础概念_基本定义
- 7.常用指令(下)v-on,v-bind,v-model的常见操作
- 中序线索二叉树
- UE4 learning records create a role and control its movement
- Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
- C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
- WordPress
- Architecture part -- the use of UMI framework and DVA
- 二进制、16进制、大端小端
- mysql版本升级+数据迁移
猜你喜欢
Hbuilderx uses the gaude map to obtain the current location
iomanip头文件在实战中的作用
使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
为什么Integer的比较最好使用equals
Kylin
Leetcode-1528- rearrange string - hash table - string
史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
二叉排序树
How does excel translate Chinese words into English automatically? This formula teaches you
Qtcreator formatting code
随机推荐
Line height for small use
CXF
final和static
Understanding of pseudo classes
Using Google protobuf protocol environment configuration in PHP
聊聊swoole或者php cli 进程如何热重启
line-height小用
Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
6.常用指令(上)v-cloak,v-once,v-pre
Gradle的环境安装与配置
util. Collection and encapsulation of JS tool functions
如何进行流程创新,以最经济的方式提升产品体验?
发送邮件工具类
Hbuilderx uses the gaude map to obtain the current location
Spark日志分析
C. Planar Reflections-CodeCraft-21 and Codeforces Round #711 (Div. 2)
php进程间传递文件描述符
对伪类的理解
Mutual conversion between QT utf8 and Unicode encoding, and the Unicode encoding output format is &xxxxx
WordPress