关于javascript:判断某个时间戳是否在当前时间北京时区的临近三个工作日内

39次阅读

共计 1670 个字符,预计需要花费 5 分钟才能阅读完成。

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);

正文完
 0