在带有日期的对象中使用星号*代替年份?

时间:2018-10-31 07:18:09

标签: javascript date

我有这段代码可以计算日期之间的天数和不参加假期的时间。

var gon = {};
gon["holiday"] = "2015-08-28,2015-09-25,2016-08-31,2016-08-07,2015-08-13,2016-08-29,2016-01-07,2015-10-31".split(",");

// 2 helper functions - moment.js is 35K minified so overkill in my opinion
function pad(num) { return ("0" + num).slice(-2); }
function formatDate(date) { var d = new Date(date), dArr = [d.getFullYear(), pad(d.getMonth() + 1), pad(d.getDate())];return dArr.join('-');}

function calculateDays(first,last) {
  var aDay = 24 * 60 * 60 * 1000,
  daysDiff = parseInt((last.getTime()-first.getTime())/aDay,10);

  if (daysDiff>0) {
    for (var i = first.getTime(), lst = last.getTime(); i <= lst; i += aDay) {
      var d = new Date(i);
      console.log(d.getDay());
      if (d.getDay() == 6 || d.getDay() == 0 // weekend
      || gon.holiday.indexOf(formatDate(d)) != -1) {
          daysDiff--;
      }
    }
  }
  return daysDiff;
}

如何使用星号*代替年份来涵盖所有年份。我不想这样

gon["holiday"] = "2018-08-28,2018-09-25,2019-08-28,2019-09-25,2020-08-28,2020-09-25,2021-08-28,2021-09-25".split(",");

我可以做这样的事情

gon["holiday"] = "*-08-28,*-09-25".split(",");

3 个答案:

答案 0 :(得分:0)

此代码可以为您完成工作:

gon["holiday"]= [...Array(10)].map((_,i) => (2015+i) + "-08-28");

结果:

(10) ["2015-08-28", "2016-08-28", "2017-08-28", "2018-08-28", "2019-08-28", "2020-08-28", "2021-08-28", "2022-08-28", "2023-08-28", "2024-08-28"]

答案 1 :(得分:0)

您可以使用findIndex并为其提供仅匹配日期和月份而不是年份的函数,如下所示:

var gon = {};
gon["holiday"] = "*-08-28,*-09-25".split(",");

function pad(num) { return ("0" + num).slice(-2); }
function formatDate(date) { var d = new Date(date), dArr = [d.getFullYear(), pad(d.getMonth() + 1), pad(d.getDate())];return dArr.join('-');}

function calculateDays(first,last) {
  var aDay = 24 * 60 * 60 * 1000,
  daysDiff = parseInt((last.getTime()-first.getTime())/aDay,10);

  if (daysDiff>0) {
    for (var i = first.getTime(), lst = last.getTime(); i <= lst; i += aDay) {
      var d = new Date(i);
      console.log(d.getDay());
      if (d.getDay() == 6 || d.getDay() == 0 // weekend
      || gon.holiday.findIndex((h)=>formatDate(d).replace(/[^-]+-/, '') == h.replace(/[^-]+-/, '')) != -1) {
          daysDiff--;
      }
    }
  }
  return daysDiff;
}

我正在使用正则表达式删除年份,它删除了第一个出现的破折号和之前的所有字符。

答案 2 :(得分:0)

您可以使用MM-DD格式的带有固定周年纪念日的假期数组:

var fixedHols = ['08-28','09-25'];

另一种随机移动的东西,例如复活节,斋月,排灯节:

var movingHols = ['YYYY-MM-DD',...]

以及在给定年份生成的基于规则的假期,例如5月的第一个星期一或11月的第一个星期一之后的星期二,等等,然后针对它们测试日期,例如

function isHoliday(date) {
  let z = n => (n<10?'0':'')+n;
  let fixedHols = ['08-28','09-25'];
  let ymd = formatDate(date);
  let md  = ymd.slice(-5);
  let movingHols = [ /* dates as YYY-MM-DD */ ];
  let rulesHols  = [ /* generate YYY-MM-DD for date.getFullYear() */ ];

  // If date is in any array return true, otherwise return false
  return [fixedHols, movingHols, rulesHols].some((hols, i) => hols.includes(i? ymd : md));
}

function formatDate(d) {
  var z = n => (n<10?'0':'')+n;
  return d.getFullYear()+'-'+z(d.getMonth()+1)+'-'+z(d.getDate());
}

[new Date(2018,7,27), // 27 Aug
 new Date(2018,7,28), // 28 Aug
 new Date(2018,7,29), // 29 Aug
 new Date(2021,7,28), // 28 Aug
 new Date(2018,8,25), // 25 Sep
 new Date(2018,8,26)] // 26 Sep
   .forEach(d => 
   console.log(`Is ${formatDate(d)} a holiday? ${isHoliday(d)?'Yes':'No'}`)
);