当前位置:网站首页>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)
}
}
结果如下:

边栏推荐
- Online customer service - charging standards and service provision of third parties
- 后序线索二叉树
- 谷歌浏览器(Chrome)最新v80版本下载
- Tree class query component
- Binary, hexadecimal, big end and small end
- WordPress
- Compiling protobuf protocol files using makefile in PHP
- 动态验证码
- iomanip头文件在实战中的作用
- Hibernate entity class curd, transaction operation summary
猜你喜欢

CXF

unsigned与signed之大白话

SSL/TLS、对称加密和非对称加密和TLSv1.3

Qtcreator formatting code

Architecture part -- the use of UMI framework and DVA
![mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动

二进制、16进制、大端小端

Hibernate core api/ configuration file / L1 cache details

line-height小用

Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
随机推荐
DPVS-FullNAT模式部署篇
Apache doris1.0 cluster setup, load balancing and parameter tuning
Leetcode-1528- rearrange string - hash table - string
Uniapp - call payment function: Alipay
CSDN add on page Jump and off page specified paragraph jump
Kotlin空指针Bug
Hibernate architecture introduction and environment construction (very detailed)
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
两种块级元素居中的方式
解析产品开发失败的5个根本原因
UE4 learning records create a role and control its movement
C. Fibonacci Words-April Fools Day Contest 2021
Using Google protobuf protocol environment configuration in PHP
51 single chip microcomputer, some registers, some knowledge points
php中使用google protobuf协议环境配置
登录拦截器
How does excel translate Chinese words into English automatically? This formula teaches you
Line height for small use
说说单例模式!
Style setting when there is a separator in the qcombobox drop-down menu