js获取本周起止日期、上周起止日期、下周起止日期,获取本月起止日期、上月起止日期、下月起止日期

9 篇文章 0 订阅
8 篇文章 2 订阅

js Date对象中setMonth()存在一些缺陷
例子:targetDate = 2022/7/31
需求:取到目标日期前1个月的日期
理想结果:2022/6/30

已下为示例代码:

var targetDate = new Date("2022/7/31");
targetDate.setMonth(targetDate.getMonth() - 1);
console.log("targetDate",targetDate);

js运行结果:2022/7/1

解决方法

function setDateMonth (num) {
    if (isNaN(num)) return NaN;
    var targetDate = new Date();
    var cy_mm = num % 12;
    var origin_mm = targetDate.getMonth();
    targetDate.setMonth(Number(targetDate.getMonth()) + Number(num));
    while(true){
        if (targetDate.getMonth() - origin_mm == cy_mm) break;
        if (origin_mm - targetDate.getMonth() == cy_mm) break;
        if (origin_mm - targetDate.getMonth() == -12 - cy_mm) break;
        if (origin_mm - targetDate.getMonth() == 12 - cy_mm) break;
        if (targetDate.getMonth() - origin_mm == 12 - cy_mm) break;
        if (targetDate.getMonth() - origin_mm == -12 - cy_mm) break;
        targetDate.setDate(targetDate.getDate() - 1);
    }
    return targetDate;
}
var targetDate = new Date("2022/7/31");
targetDate.setDateMonth(- 1);
console.log("targetDate",targetDate); //2022/6/30

下面两个方法有缺陷 需要结合上面 setDateMonth方法重新实现


/** 
* 获得相对当前周AddWeekCount个周的起止日期 
* AddWeekCount为0代表当前周  为-1代表上一个周  为1代表下一个周以此类推
* **/ 
function getWeekStartAndEnd(AddWeekCount) { 
//起止日期数组  
	var startStop = new Array(); 
	//一天的毫秒数  
	var millisecond = 1000 * 60 * 60 * 24; 
	//获取当前时间  
	var currentDate = new Date();
	//相对于当前日期AddWeekCount个周的日期
	currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
	//返回date是一周中的某一天
	var week = currentDate.getDay(); 
	//返回date是一个月中的某一天  
	var month = currentDate.getDate();
	//减去的天数  
	var minusDay = week != 0 ? week - 1 : 6; 
	//获得当前周的第一天  
	var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay)); 
	//获得当前周的最后一天
	 var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
	//添加至数组  
	startStop.push(getDateStr3(currentWeekFirstDay)); 
	startStop.push(getDateStr3(currentWeekLastDay)); 
	
	return startStop; 
} 
/** 
* 获得相对当月AddMonthCount个月的起止日期 
* AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
* ***/ 
function getMonthStartAndEnd(AddMonthCount) { 
	//起止日期数组  
	var startStop = new Array(); 
	//获取当前时间  
	var currentDate = new Date();
	var month=currentDate.getMonth()+AddMonthCount;
	if(month<0){
	  var n = parseInt((-month)/12);
	  month += n*12;
	  currentDate.setFullYear(currentDate.getFullYear()-n);
	}
	currentDate = new Date(currentDate.setMonth(month));
	//获得当前月份0-11  
	var currentMonth = currentDate.getMonth(); 
	//获得当前年份4位年  
	var currentYear = currentDate.getFullYear(); 
	//获得上一个月的第一天  
	var currentMonthFirstDay = new Date(currentYear, currentMonth,1); 
	//获得上一月的最后一天  
	var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0); 
	//添加至数组  
	startStop.push(getDateStr3(currentMonthFirstDay)); 
	startStop.push(getDateStr3(currentMonthLastDay)); 
	//返回  
	return startStop; 
}


//获取当前日期yy-mm-dd
//date 为时间对象
function getDateStr3(date) {
	var year = "";
	var month = "";
	var day = "";
	var now = date;
	year = ""+now.getFullYear();
	if((now.getMonth()+1)<10){
	  month = "0"+(now.getMonth()+1);
	}else{
	  month = ""+(now.getMonth()+1);
	}
	if((now.getDate())<10){
	  day = "0"+(now.getDate());
	}else{
	  day = ""+(now.getDate());
	}
	return year+"-"+month+"-"+day;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python的datetime实现本周上周本月上月起止日期的代码: ```python import datetime # 本周起止日期 now = datetime.datetime.now() start_of_week = now - datetime.timedelta(days=now.weekday()) end_of_week = start_of_week + datetime.timedelta(days=6) # 上周起止日期 start_of_last_week = start_of_week - datetime.timedelta(days=7) end_of_last_week = end_of_week - datetime.timedelta(days=7) # 本月起止日期 start_of_month = datetime.datetime(now.year, now.month, 1) if now.month == 12: end_of_month = datetime.datetime(now.year+1, 1, 1) - datetime.timedelta(days=1) else: end_of_month = datetime.datetime(now.year, now.month+1, 1) - datetime.timedelta(days=1) # 上月起止日期 last_month = now.month - 1 if now.month > 1 else 12 last_year = now.year - 1 if last_month == 12 else now.year start_of_last_month = datetime.datetime(last_year, last_month, 1) end_of_last_month = datetime.datetime(now.year, now.month, 1) - datetime.timedelta(days=1) # 输出起止日期 print("本周起止日期:", start_of_week.date(), "-", end_of_week.date()) print("上周起止日期:", start_of_last_week.date(), "-", end_of_last_week.date()) print("本月起止日期:", start_of_month.date(), "-", end_of_month.date()) print("上月起止日期:", start_of_last_month.date(), "-", end_of_last_month.date()) ``` 其中关键的方法是`datetime.timedelta()`表示时间差,例如`datetime.timedelta(days=7)`表示7天时间差。`datetime.datetime()`则表示一个具体的日期时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值