欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)的一個比較不錯的判斷節(jié)假日的實(shí)現(xiàn)代碼(假日包括周末,不包括調(diào)休上班的周末)

 更新時間:2024年12月22日 11:41:14   作者:勤思而敏學(xué)  
最近需要實(shí)現(xiàn)一個判斷今天是不是節(jié)假日的功能,考慮周末與法定節(jié)假日調(diào)休等,這樣的比較完整的功能就需要下面的代碼來實(shí)現(xiàn)了

思路:創(chuàng)建兩個數(shù)組,數(shù)組1為節(jié)假日數(shù)組,數(shù)組2為是周末上班日期數(shù)組。如果當(dāng)前日期(或某日期)同時滿足2個條件(1.在節(jié)假日數(shù)組內(nèi)或在周末。2.不在周末上班日期數(shù)組)即為節(jié)假日,否則即為非假日。

注意:兩個數(shù)組日期為2024-4-4(而不是2024-04-04),因?yàn)閏urdate.getMonth()和curdate.getDate()返回的是4,而不是04。

//節(jié)假日數(shù)組
var holidays = [
  '2024-4-4', //清明
  '2024-4-5', //清明
  '2024-4-6', //清明
  '2024-5-1', // 勞動節(jié)
  '2024-5-2', // 勞動節(jié)
  '2024-5-3', // 勞動節(jié)
  '2024-5-4', // 勞動節(jié)
  '2024-5-5', // 勞動節(jié)
  '2024-6-8', // 端午節(jié)
  '2024-6-9', // 端午節(jié)
  '2024-6-10', // 端午節(jié)
  '2024-9-15', // 中秋節(jié)
  '2024-9-16', // 中秋節(jié)
  '2024-9-17', // 中秋節(jié)
  '2024-10-1', // 國慶節(jié)
  '2024-10-2', // 國慶節(jié)
  '2024-10-3', // 國慶節(jié)
  '2024-10-4', // 國慶節(jié)
  '2024-10-5', // 國慶節(jié)
  '2024-10-6', // 國慶節(jié)
  '2024-10-7', // 國慶節(jié)
  '2023-12-31'  // 元旦
];
//周末上班日期數(shù)組
var nWeekend = [
 '2024-4-7', //清明調(diào)整
 '2024-4-28', //五一調(diào)整
 '2024-5-11', //五一調(diào)整
 '2024-9-14', //中秋調(diào)整
 '2024-9-29', //國慶調(diào)整
 '2024-10-12', //國慶調(diào)整
];
 
var curdate = new Date();
curdate.setTime(curdate.getTime() + 4 * 24 * 60 * 60 * 1000); // 1即明天,2即后天
var year = curdate.getFullYear();
var month = curdate.getMonth()+1 ;//getMonth()+1為當(dāng)前月份
var date = curdate.getDate();
var formattedDate = year + "-" + month + "-" + date;
 
//該日期同時滿足2個條件即為節(jié)假日:1.在節(jié)假日數(shù)組內(nèi)或在周末. 2.不在周末上班日期數(shù)組
if((holidays.indexOf(formattedDate)>=0 || (curdate.getDay()==0 || curdate.getDay()==6)) && nWeekend.indexOf(formattedDate)<0){
    console.log(formattedDate+':'+'節(jié)假日');
}else{
    console.log(formattedDate+':'+'非節(jié)假日');
}

 如下,當(dāng)前日期2024-4-3:非節(jié)假日。

 如下,當(dāng)前日期加1為2024-4-4:節(jié)假日(清明節(jié))。

 如下,當(dāng)前日期加4為2024-4-7:非節(jié)假日(雖是周日,但屬于清明節(jié)調(diào)整的上班日)。

js 實(shí)現(xiàn)判斷節(jié)假日

var date = new Date();
 
// 獲取月份(注意月份從0開始計數(shù))
var month = date.getMonth() + 1; // 需要加上1才能得到正確的月份值
 
// 根據(jù)不同的月份設(shè)置相應(yīng)的節(jié)日信息
if (month === 1 && date.getDate() === 1) {
    console.log("今天是元旦");
} else if (month === 2 && date.getDate() === 14) {
    console.log("今天是情人節(jié)");
} else if (month === 3 && date.getDate() === 8) {
    console.log("今天是婦女節(jié)");
} else if (month === 4 && date.getDate() === 4) {
    console.log("今天是清明節(jié)");
} else if (month === 5 && date.getDate() === 1) {
    console.log("今天是勞動節(jié)");
} else if (month === 5 && date.getDay() === 0) {
    console.log("今天是母親節(jié)");
} else if (month === 6 && date.getDay() === 0) {
    console.log("今天是父親節(jié)");
} else if (month === 6 && date.getDate() >= 24 && date.getDate() <= 27) {
    console.log("今天是端午節(jié)");
} else if (month === 9 && date.getDate() === 10) {
    console.log("今天是教師節(jié)");
} else if (month === 10 && date.getDate() === 1) {
    console.log("今天是國慶節(jié)");
} else if (month === 12 && date.getDate() === 25) {
    console.log("今天是圣誕節(jié)");
} else {
    console.log("今天沒有特定的節(jié)日");
}

js 實(shí)現(xiàn)判斷節(jié)假日2

在JavaScript中,判斷節(jié)假日通常需要一個已知的節(jié)假日列表,然后將日期與這個列表進(jìn)行比較。以下是一個簡單的實(shí)現(xiàn)示例:

// 假設(shè)的節(jié)假日列表
const holidays = [
  { date: '2024-01-01', name: '元旦' },
  { date: '2024-02-15', name: '春節(jié)' },
  { date: '2024-04-05', name: '清明節(jié)' },
  // ... 其他節(jié)假日
];
 
// 判斷給定日期是否為節(jié)假日的函數(shù)
function isHoliday(date) {
  const dateString = date.toISOString().split('T')[0];
  return holidays.some(holiday => holiday.date === dateString);
}
 
// 使用示例
const testDate = new Date('2023-02-15');
console.log(isHoliday(testDate)); // 輸出: true
 
const anotherDate = new Date('2023-03-01');
console.log(isHoliday(anotherDate)); // 輸出: false

在這個示例中,holidays 數(shù)組包含了2023年的部分節(jié)假日。isHoliday 函數(shù)接收一個Date對象作為參數(shù),將其轉(zhuǎn)換為字符串格式的日期,然后與預(yù)定義的節(jié)假日列表中的日期進(jìn)行比較。如果找到匹配項(xiàng),則返回true,表示給定的日期是節(jié)假日;否則返回false。

javascript中Date對象周六日判斷

// 創(chuàng)建一個Date對象,使用當(dāng)前時間
const currentDate = new Date();

// 獲取星期幾的值
const dayOfWeek = currentDate.getDay();

// 判斷當(dāng)前日期是否為周六或周日,并輸出結(jié)果
if (dayOfWeek === 6) {
    console.log("今天是周六。");
} else if (dayOfWeek === 0) {
    console.log("今天是周日。");
} else {
    console.log("今天不是周末。"

或者

function isWeekend() {
  const date = new Date();
  const day = date.getDay();
  return day === 0 || day === 6; // 0代表周日,6代表周六
}
 

實(shí)際應(yīng)用

function iswktime() {
	const date = new Date();
	const day = date.getDay();
	var isweekday=false;
	if (day===0 || day === 6){
		isweekday=true;
		}
	if (isweekday===true) {
		return false;
		}else{
			if ((date.getHours() >= 8 && date.getHours() <= 18)) {
				return true;
			} else {
				return false;
			}
}
}

到此這篇關(guān)于JS實(shí)現(xiàn)的一個比較不錯的判斷節(jié)假日的實(shí)現(xiàn)代碼(假日包括周末,不包括調(diào)休上班的周末)的文章就介紹到這了,更多相關(guān)JS判斷節(jié)假日內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論