JS判断当前选择日期是否为今日或本周或本月

判断当前选择日期是否为今日

/*判断当前选择日期是否为今日
代码*/
let now=new Date()
let today=now.toISOString().substring(0,10)
now=new Date(today)
let ADate='2020-01-10'
let DDate='2020-03-31'
console.log('判断今日:');
console.log('今日日期:'+today);
console.log('参考日期:');
console.log('ADate:'+ADate);
console.log('DDate:'+DDate);
if(today==ADate){
	console.log('ADate今日');
}else{
	console.log('ADate非今日');
}
if(today==DDate){
	console.log('DDate今日');
}else{
	console.log('DDate非今日');
}
/*结果*/
判断今日:
今日日期:2020-03-31
参考日期:
ADate:2020-01-10
DDate:2020-03-31
ADate非今日
DDate今日

判断当前选择日期是否为本周

/*判断当前选择日期是否为本周
代码*/
let now=new Date()
let today=now.toISOString().substring(0,10)
let ADate='2020-03-29'
let BDate='2020-03-30'
let CDate='2020-04-05'
let DDate='2020-04-06'
let a=new Date(ADate)
let b=new Date(BDate)
let c=new Date(CDate)
let d=new Date(DDate)
let firstdayoffset=(now.getDay()==0?7:now.getDay())-1
console.log('判断本周:');
console.log('今日日期:'+today);
console.log('今日星期:'+(now.getDay()==0?7:now.getDay()));
console.log('距离本周第一天的天数'+firstdayoffset);
console.log('参考日期:');
console.log('ADate:'+ADate);
console.log('BDate:'+BDate);
console.log('CDate:'+CDate);
console.log('DDate:'+DDate);
now.setDate(now.getDate()-firstdayoffset-1)
let firstday=now.toISOString().substring(0,10)
console.log('本周第一天-1:'+firstday);
firstday=new Date(firstday)
now.setDate(now.getDate()+8)
let lastday=now.toISOString().substring(0,10)
console.log('本周最后一天+1:'+lastday);
lastday=new Date(lastday)
if(a>firstday&&a<lastday){
	console.log('ADate本周');
}else{
	console.log('ADate非本周');
}
if(b>firstday&&b<lastday){
	console.log('BDate本周');
}else{
	console.log('BDate非本周');
}
if(c>firstday&&c<lastday){
	console.log('CDate本周');
}else{
	console.log('CDate非本周');
}
if(d>firstday&&d<lastday){
	console.log('DDate本周');
}else{
	console.log('DDate非本周');
}
/*结果*/
判断本周:
今日日期:2020-03-31
今日星期:2
距离本周第一天的天数1
参考日期:
ADate:2020-03-29
BDate:2020-03-30
CDate:2020-04-05
DDate:2020-04-06
本周第一天-1:2020-03-29
本周最后一天+1:2020-04-06
ADate非本周
BDate本周
CDate本周
DDate非本周

判断当前选择日期是否为本月

/*判断当前选择日期是否为本月
代码*/
let now=new Date()
let today=now.toISOString().substring(0,7)
console.log('判断本月:');
console.log('本月日期:'+today);
let ADate='2020-01-29'
let BDate='2020-02-30'
let CDate='2020-03-05'
let DDate='2020-12-06'
console.log('参考日期:');
ADate=ADate.substring(0,7)
BDate=BDate.substring(0,7)
CDate=CDate.substring(0,7)
DDate=DDate.substring(0,7)
console.log('ADate:'+ADate);
console.log('BDate:'+BDate);
console.log('CDate:'+CDate);
console.log('DDate:'+DDate);
if(today==ADate){
	console.log('ADate本月');
}else{
	console.log('ADate非本月');
}
if(today==BDate){
	console.log('BDate本月');
}else{
	console.log('BDate非本月');
}
if(today==CDate){
	console.log('CDate本月');
}else{
	console.log('CDate非本月');
}
if(today==DDate){
	console.log('DDate本月');
}else{
	console.log('DDate非本月');
}
/*结果*/
判断本月:
本月日期:2020-03
参考日期:
ADate:2020-01
BDate:2020-02
CDate:2020-03
DDate:2020-12
ADate非本月
BDate非本月
CDate本月
DDate非本月
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值