如何计算仅给出月份的日期,该月份的周数和星期几

时间:2018-03-07 21:22:20

标签: javascript date

我正在调用一个api,返回以下对象,告诉我夏令时从3月11日开始,给定时区:

{
  dayOfWeek: "Sunday",
  month: 3,
  timeOfDay: "0001-01-01T02:00:00.000Z",
  week: 2
}

其中一周:在这种情况下,2名代表是3月的第2周。

如何创建3月11日?我有以下功能,可以在3月份正常工作,但是如果说api返回以下内容,我意识到4月会失败:

{
  dayOfWeek: "Saturday",
  month: 4,
  timeOfDay: "0001-01-01T02:00:00.000Z",
  week: 2
}

代码:

/**
 * Given the year, month, week day, week number of the month, and time, calculate a date
    - set the date to the first day of the month
    - then adjust the date backwards to the proper weekday
    - then move it forward by 7 * week number to get it to the proper date
 */
calcDateFromDaylightSavingRulesAPI: function(year, month, time, dayOfWeek, weekNum) {
    const weekDays = {
        Sunday: 0,
        Monday: 1,
        Tuesday: 2,
        Wednesday: 3,
        Thursday: 4,
        Friday: 5,
        Saturday: 6
    };

    const date = new Date(year, month, 1, time);
    date.setDate(date.getDate() + weekDays[dayOfWeek] - date.getDay());
    date.setDate(date.getDate() + (weekNum * constants.DAYS_PER_WEEK));

    return date;

2 个答案:

答案 0 :(得分:1)

如果您只想要一个月中某一天的第n次出现,您可以获得该月的第一天,移至特定日期的第一次出现,然后添加(n-1) * 7天以到达第n天发生,例如

function getNthDay(year, month, dayName, n) {
  var weekDays = {
        Sunday: 0,
        Monday: 1,
        Tuesday: 2,
        Wednesday: 3,
        Thursday: 4,
        Friday: 5,
        Saturday: 6
    };
  // validate input values here, throw errors if required, e.g.
  // dayName must be in weekdays, 0 < n < 6 

  // Create date for first day of required month
  var d = new Date(year, month - 1, 1);
  
  // Set to first instance of particular day
  d.setDate((8 - (d.getDay() - weekDays[dayName]))%7);
  
  // Add (n-1)*7 days
  d.setDate(d.getDate() + (n-1) * 7);
  
  // Check final date is still in required month
  return d.getMonth() == month - 1? d : 'fail';
}

// Second Sunday in March, 2018
console.log(getNthDay(2018,3,'Sunday',2).toString())

如果你想要第二周的星期日,这有点难以计算,因为有些地方星期天开始几周,有些星期一开始。此外,该月周的算法可能会从一个地方变为另一个地方(如同一年中的一周)。

答案 1 :(得分:0)

我认为这现在正常运作。谢谢大家的帮助!

/**
 * Given an object which contains month, time, week day, and week number in a month, calculate a date
    - set the date to the first day of the month
    - then adjust the date backwards to the proper weekday
    - then move it forward by 7 * week number to get it to the proper date
 */
calcDateFromDaylightSavingRulesAPI: function(ruleSet) {
    const today = new Date();
    const year = today.getFullYear();
    const month = ruleSet.month - 1;
    const time = new Date(ruleSet.timeOfDay).getUTCHours();
    const dayOfWeek = ruleSet.dayOfWeek;
    const weekNum = ruleSet.week;

    const weekDays = {
        Sunday: 0,
        Monday: 1,
        Tuesday: 2,
        Wednesday: 3,
        Thursday: 4,
        Friday: 5,
        Saturday: 6
    };

    const date = new Date(year, month, 1, time);
    const diff = weekDays[dayOfWeek] - date.getDay();
    date.setDate(date.getDate() + (diff >= 0 ? diff - 7 : diff));
    date.setDate(date.getDate() + (weekNum * constants.DAYS_PER_WEEK));

    return date;
},