首页 > 网站 > WEB开发 > 正文

通过函数计算任意日期所在月和周的起始时间

2024-04-27 15:07:02
字体:
来源:转载
供稿:网友
//初始化一个时间date=2016-12-13,通过函数计算所在月和周的起始时间function month(y,m,d){var now = new Date(y,m-1,d);var year = now.getFullYear();var month = now.getMonth()+1;var date = now.getDate();var result;if(month == 2){if(year%400 == 0 || (year%4 == 0 && year%100 != 0)){result =29; }else{result =28;}}else{switch (month){case 4:case 6:case 8:case 9:case 11:result = 30;break;default:result = 31;break;}}return "本月开始日期是" + year + "-" + month + "-01,结束日期是" + year + "-" + month + "-" +result;}var x = month(2017,2,13);console.log(x);function week(y,m,d){var now = new Date(y,m-1,d);var day = now.getDay();if(day==0){day = 7;}var time = now.getTime();var startTime = time - (day-1)*24*60*60*1000;var endTime = time + (7 - day)*24*60*60*1000;var start = new Date(startTime).toLocaleDateString().split("/");var end = new Date(endTime).toLocaleDateString().split("/");if(start[1]<10){start[1] = "0"+start[1];}if(start[2]<10){start[2] = "0"+start[2];}if(end[1]<10){end[1] = "0"+end[1];}if(end[2]<10){end[2] = "0"+end[2];}console.log("本周开始的日期"+start[0]+"-"+start[1]+"-"+start[2]+",结束日期"+end[0]+"-"+end[1]+"-"+end[2]);}week(2016,2,5);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表