js最近一个节假日获取

	节假日,周末时间查询
	var festival = ["1-1","2-4","2-5","2-6","2-7","2-8","2-9","2-10","4-5","4-6","4-7",
		"5-1","5-2","5-3","5-4","6-7","6-8","6-9","9-13","9-14","9-15","10-1","10-2","10-3","10-4","10-5","10-6","10-7"];	
	var beginTime = $("#holiday-grid-toolbar").find("input[textboxname='beginTime']");
	var endTime = $("#holiday-grid-toolbar").find("input[textboxname='endTime']"); 
	formatterDate = function (con) {
		var date1 = new Date();
		var week = date1.getDay();
		var date = null;
		if(con){
			week = week+1; 
			date = new Date(date1.getTime()-week*24*60*60*1000);
			for(var i = 0;i<=week;i++){
				var date2 = new Date(date1.getTime()-i*24*60*60*1000);
				if(festival.indexOf((date2.getMonth() + 1)+"-"+date2.getDate())!=-1){
					var j = 0;
					while(true){
						date = new Date(date2.getTime()-j*24*60*60*1000);
						j++;
						if(festival.indexOf((date.getMonth() + 1)+"-"+date.getDate())==-1){
							break;
						}
					}
					date = new Date(date.getTime()+24*60*60*1000);
					break;
				}
			}
		}else{
			date = new Date(date1.getTime()-week*24*60*60*1000);
			for(var i = 0;i<week;i++){
				var date2 = new Date(date1.getTime()-i*24*60*60*1000);
				if(festival.indexOf((date2.getMonth() + 1)+"-"+date2.getDate())!=-1){
					date = new Date(date1.getTime()-i*24*60*60*1000);
					break;
				}
			}
		}
		var day = date.getDate()> 9 ? date.getDate() : "0" + (date.getDate());
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
		+ (date.getMonth() + 1);
		return date.getFullYear() + '-' + month + '-' + day;
	};
	beginTime.datebox('setValue',formatterDate(true));
	endTime.datebox('setValue', formatterDate(false));
以下是获取一年内节假日JS代码: ```javascript // 获取当前时间 const currentDate = new Date(); const currentYear = currentDate.getFullYear(); // 当前年份 // 节假日列表 const holidayList = []; // 聚合数据节假日API的请求地址(需要替换为自己的APP Key) const apiUrl = `http://v.juhe.cn/calendar/year?year=${currentYear}&key=your_app_key`; // 发送API请求并处理返回的节假日数据 fetch(apiUrl) .then(response => response.json()) .then(data => { const holidayData = data.result.data.holiday; for (let key in holidayData) { if (holidayData.hasOwnProperty(key)) { const holiday = holidayData[key]; const startDate = new Date(holiday.startday.replace(/-/g, "/")); const endDate = new Date(holiday.endday.replace(/-/g, "/")); holidayList.push({ name: holiday.name, start: startDate, end: endDate }); } } }) .catch(error => { console.error('Error:', error); }); // 显示一年内的节假日 const oneYearLater = new Date(currentYear + 1, 0, 1); // 一年后的日期 let currentDate = new Date(currentYear, 0, 1); // 当前日期 while (currentDate < oneYearLater) { let isHoliday = false; for (let i = 0; i < holidayList.length; i++) { const holiday = holidayList[i]; if (currentDate >= holiday.start && currentDate <= holiday.end) { isHoliday = true; console.log(`${holiday.name}: ${currentDate.toLocaleDateString()}`); break; } } if (!isHoliday) { console.log(currentDate.toLocaleDateString()); } currentDate.setDate(currentDate.getDate() + 1); } ``` 注意,使用聚合数据节假日API时需要替换为自己的APP Key。并且,由于API请求是异步的,所以需要在API请求完成后再显示节假日列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值