如何在我的情况下获得假期?

时间:2014-12-08 02:15:20

标签: javascript date object

我正在努力获得给定日期范围的假期。

例如:

var holiday = ['2014-01-01','2014-07-04','2014-12-24', '2014-12-25'...other holidays elements ];

var startDate = '2014-08-01'
var endDate = '2014-12-30';

var holidays=getHolidays(startDate, endDate);
//console.log(holidays) -> output 2014-12-24 and 2014-12-25.

getHolidays = function(startDate, endDate) {
    var holidays=[];
    //not sure how to get holidays here....
    return holidays
}

我希望写一个功能来获得2014-12-24和2014-12-25。我的大脑现在已经被炒了,我不知道最好的方法是什么。任何人都可以帮我吗?非常感谢!

3 个答案:

答案 0 :(得分:3)

解决方案是使用数组的过滤功能。第一个参数是您需要过滤的数组,在您的情况下,它是['2014-01-01','2014-07-04','2014-12-24','2014-12-25'] 。

var getHolidays = function(holiday,startDate, endDate) {
    return holiday.filter(function(element){
        var start = new Date(startDate);
        var end = new Date(endDate);
        var temp = new Date(element);
        return temp >= start && temp <= end;
    });
}

var holiday = ['2014-01-01', '2014-07-04', '2014-12-24', '2014-12-25'];
var startDate = '2014-08-01'
var endDate = '2014-12-30';
var holidays = getHolidays(holiday,startDate, endDate); //["2014-12-24", "2014-12-25"]

它的工作方式与您所期望的完全相同 JSFiddle

答案 1 :(得分:1)

您可以尝试将它们转换为日期对象,然后以这种方式进行比较。类似的东西:

var holidays = ['2014-01-01','2014-07-04','2014-12-24', '2014-12-25'];

var startDate = '2014-08-01';
var endDate = '2014-12-30';

var getHolidays = function(startDate, endDate) {
    var startTime = new Date(startDate).getTime();
    var endTime = new Date(endDate).getTime();
    var output = [];

    holidays.forEach(function(holiday) {
        var holidayTime = new Date(holiday).getTime();

        if (holidayTime >= startTime && holidayTime <= endTime) {
            output.push(holiday);
        }
    });

    return output;
}


var holidays = getHolidays(startDate, endDate);

console.log(holidays);

答案 2 :(得分:0)

作为普通字符串, getHolidays 函数可以是:

// Get holidays between two dates
function getHolidays(startDate, endDate) {
  var output = [];
  var i = 0;
  var holiday;

  while ((holiday = holidays[i++]) && holiday < endDate) {
    if (startDate <= holiday) {
      output.push(holiday)
    }
  }
  return output;
}

如果您使用的是ISO 8601日期字符串,则无需将它们转换为Date对象。 ISO格式的原因之一是,在大多数系统中,您可以直接比较字符串而不是Date对象或某个数字值。

您还可以将字符串解析为Date对象,然后使用它们进行计算,但需要更多代码才能获得真正的好处:

// Parse date string in y-m-d format
function parseYMD(s) {
  var b = s.split(/\D/);
  var d = new Date();
  d.setHours(0,0,0,0);
  d.setFullYear(b[0], --b[1], b[2]);
  return d.getFullYear() == b[0] && d.getDate() == b[2]? d : NaN;
}

// Holiday array
var holidays = ['2014-01-01','2014-07-04','2014-12-24', '2014-12-25'];

// Get holidays between two dates
function getHolidays(startDate, endDate) {
  startDate = parseYMD(startDate);
  endDate   = parseYMD(endDate);
  var output = [];
  var i = 0;
  var holiday;

  for (var i=0, iLen=holidays.length; i<iLen; i++) {
    holiday = parseYMD(holidays[i]);

    if (holiday <= endDate) {
      if (holiday >= startDate) {
        output.push(holidays[i]);
      }
    }
  }
  return output; 
}

console.log(getHolidays('2014-08-01', '2014-12-30')); // ["2014-12-24", "2014-12-25"]