【问题标题】:Get the days in a Week in Javascript, Week starts on Sunday用 Javascript 获取一周中的天数,一周从星期日开始
【发布时间】:2011-10-07 11:57:42
【问题描述】:

这是用户给定的输入:

年 = 2011,月 = 3(三月),周 = 2

我想用 JavaScript 获取 2011 年 3 月第 2 周的日期。

例如6日星期日、7日星期一、8日星期二、9日星期三、10日星期四、11日星期五、12日星期六

有什么想法吗?谢谢!

【问题讨论】:

  • 你如何定义一个月的第一周?是第一个月的那一周吗?在这种情况下,它也可能是上个月的最后一周,这可能会令人困惑。在许多地方,一周的第一天是星期一,而不是星期日。
  • @RobG ".. 一周的第一天是星期一,而不是星期日.." 如果我​​们基于 ISO,那就对了。但这也取决于您居住在哪个国家/地区。大多数亚洲国家每周日从那里开始。我从星期天开始我的一周,因为这是我的客户告诉我的。

标签: javascript datetime date datepicker dayofweek


【解决方案1】:

给定

var year = 2011;
var month = 3;
var week = 2;

var firstDateOfMonth = new Date(year, month - 1, 1); // Date: year-month-01

var firstDayOfMonth = firstDateOfMonth.getDay();     // 0 (Sun) to 6 (Sat)

var firstDateOfWeek = new Date(firstDateOfMonth);    // copy firstDateOfMonth

firstDateOfWeek.setDate(                             // move the Date object
    firstDateOfWeek.getDate() +                      // forward by the number of
    (firstDayOfMonth ? 7 - firstDayOfMonth : 0)      // days needed to go to
);                                                   // Sunday, if necessary

firstDateOfWeek.setDate(                             // move the Date object
    firstDateOfWeek.getDate() +                      // forward by the number of
    7 * (week - 1)                                   // weeks required (week - 1)
);

var dateNumbersOfMonthOnWeek = [];                   // output array of date #s
var datesOfMonthOnWeek = [];                         // output array of Dates

for (var i = 0; i < 7; i++) {                        // for seven days...

    dateNumbersOfMonthOnWeek.push(                   // push the date number on
        firstDateOfWeek.getDate());                  // the end of the array

    datesOfMonthOnWeek.push(                         // push the date object on
        new Date(+firstDateOfWeek));                 // the end of the array

    firstDateOfWeek.setDate(
        firstDateOfWeek.getDate() + 1);              // move to the next day

}

然后

  • dateNumbersOfMonthOnWeek 将包含该周的日期编号。
  • datesOfMonthOnWeek 将包含该周的日期对象。

虽然这对于这项工作来说似乎有点过头了,但其中大部分是让它在所有情况下都能正常工作的必要条件,例如当日期数字跨越另一个月份时。不过,我确信它可以被优化为不那么冗长。

【讨论】:

  • 那真是太好了。你能向我解释更多关于 setDate 的信息吗,如果你没问题的话。对不起,我是 javascript 的初学者。非常感谢。
  • Date::setDate 接受一个参数,这是一个“日期编号”,用于将对象的月份日期更改为。从某种意义上说,将值传递给 35 会将日期更改为下个月(几天后),而不是导致错误,这是一种智能且有用的方法。
  • 哦,我明白了,一个问题,虽然一些工作日落入其他月份。我会尝试自己修复它。再次感谢。
  • 我已经在某些情况下测试了我的代码,其中一些结果天进入下个月。它有效。
  • 完成,我现在已经修好了。新代码是否按预期工作?
【解决方案2】:

我通常使用Datejs 在 js 中进行日期操作。我会这样做:

var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
var lastDayOfWeek = firstDayOfWeek.addDays(6);

【讨论】:

    【解决方案3】:

    假设该月的第一周是该月的第一周,那么以下函数将返回给定年月周号的一周第一天的日期:

    /* 
       Input year, month and week number from 1
       Returns first day in week (Sunday) where
       first week is the first with 1st of month in it
    */
    function getFirstDayOfWeek(y, m, w) {
      var d = new Date(y, --m);
      d.setDate(--w * 7 - d.getDay() + 1);
      return d;
    }
    
    alert(getFirstDayOfWeek(2011,3, 2)); // Sun Mar 06 2011
    

    要获得剩余的日子,只需循环 6 次,每次将日期加一,例如

    function getWeekDates(y, m, w) {
      var d = getFirstDayOfWeek(y, m, w)
      var week = [new Date(d)];
      var i = 6;
      while (i--) {
        week.push(new Date(d.setDate(d.getDate() + 1)));
      }
      return week;
    }
    
    // Show week of dates
    var week = getWeekDates(2011,3, 2);
    for (var i=0, iLen=week.length; i<iLen; i++) {
      alert(week[i]);
    }
    

    【讨论】:

    • 感谢您回答 robG...我现在真的很匆忙。我无法检查您的代码。
    • @RobG:我已经测试了你的代码并遇到了一个问题。我还使用 Chrome 上的控制台来查看结果。详细信息:Week number: 5 Week: Sun Jan 26 2014 00:00:00 GMT+0700 (SE Asia Standard Time), Mon Jan 27 2014 00:00:00 GMT+0700 (SE Asia Standard Time),..., Sat Feb 01 2014 00:00:00 GMT+0700 (SE Asia Standard Time) Week number: 6 Week: Sun Mar 02 2014 00:00:00 GMT+0700 (SE Asia Standard Time), Mon Mar 03 2014 00:00:00 GMT+0700 (SE Asia Standard Time),...,Sat Mar 08 2014 00:00:00 GMT+0700 (SE Asia Standard Time) 从 01/02/2014(第 5 周)到 02/03/2014(第 6 周)有一个问题。请修复它。
    【解决方案4】:

    对第一个对我有用的答案稍作修改:

    year = 2014;
    week = 31;//week number is 31 for the example, could be 120.. it will just jump trough years
    // get the date for the first day of the year               
    var firstDateOfYear = new Date(year,0,1);
    // set the date to the number of days for the number of weeks
    firstDateOfYear.setDate(firstDateOfYear.getDate()+(7 * (week-1))); 
    // get the number of the day in the week 0 (Sun) to 6 (Sat)
    var counter = firstDateOfYear.getDay();
    
    //make sunday the first day of the week
    for(i=0;i<counter;i++){
        firstDateOfYear.setDate(firstDateOfYear.getDate()-1)
    }
    
    var firstDateOfWeek = new Date(firstDateOfYear);    // copy firstDateOfYear
    
    var dateNumbersOfMonthOnWeek = [];                   // output array of date #s
    var datesOfMonthOnWeek = [];                         // output array of Dates
    
    for (var i = 0; i < 7; i++) {                        // for seven days...
        dateNumbersOfMonthOnWeek.push(                   // push the date number on
            firstDateOfWeek.getDate());                  // the end of the array
    
     datesOfMonthOnWeek.push(                         // push the date object on
         new Date(+firstDateOfWeek));                 // the end of the array
    
     firstDateOfWeek.setDate(
        firstDateOfWeek.getDate() + 1);              // move to the next day
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      相关资源
      最近更新 更多