function isWithinThreeWorkingDays(targetTimestamp) {
// 將目標時間戳調整到北京時區
var targetTimeInBeijing = targetTimestamp + 8 * 60 * 60 * 1000;
// 獲取當前時間的時間戳,並調整到北京時區
var currentTime = new Date().getTime();
var currentTimeInBeijing = currentTime + 8 * 60 * 60 * 1000;
// 獲取目標日期的年、月、日
var targetDate = new Date(targetTimeInBeijing);
var targetYear = targetDate.getFullYear();
var targetMonth = targetDate.getMonth();
var targetDay = targetDate.getDate();
// 獲取當前日期的年、月、日
var currentDate = new Date(currentTimeInBeijing);
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
// 計算當前時間與目標時間的天數差
var dayDiff = Math.floor((targetTimeInBeijing - currentTimeInBeijing) / (24 * 60 * 60 * 1000));
// 判斷是否在當前時間的臨近三個工作日內
if (currentYear === targetYear && currentMonth === targetMonth) {
var targetWeekday = targetDate.getDay();
var currentWeekday = currentDate.getDay();
// 判斷目標日期是否為週六或週日
if (targetWeekday !== 0 && targetWeekday !== 6) {
// 判斷當前日期是否為週六或週日
if (currentWeekday !== 0 && currentWeekday !== 6) {
var remainingDays = targetDay - currentDay;
// 判斷是否在臨近三個工作日內
if (remainingDays >= 0 && remainingDays <= 3) {
return true;
}
} else if (currentWeekday === 6 && dayDiff <= 2) {
return true;
} else if (currentWeekday === 0 && dayDiff <= 1) {
return true;
}
} else if (targetWeekday === 6 && dayDiff <= 1) {
return true;
} else if (targetWeekday === 0 && dayDiff === 0) {
return true;
}
} else if (currentYear === targetYear && currentMonth === targetMonth - 1) {
var remainingDaysInCurrentMonth = new Date(currentYear, currentMonth + 1, 0).getDate() - currentDay;
var remainingDaysInTargetMonth = targetDay;
// 判斷是否在臨近三個工作日內(跨越週末)
if (remainingDaysInCurrentMonth + remainingDaysInTargetMonth <= 3) {
return true;
}
}
return false;
}
// 示例使用
var timestamp = 1677635721000; // 給定的時間戳,2023-06-29 16:35:21
var result = isWithinThreeWorkingDays(timestamp);
console.log(result);