用 Javascript 获取一周中的天数,一周从周日开始

发布于 2024-11-24 15:57:31 字数 182 浏览 2 评论 0原文

以下是用户给出的输入:

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

我想在 JavaScript 中获取 2011 年 3 月第 2 周的天数。

例如,周日 6 号、周一 7 号、周二 8 号、周三 9 号、周四 10 号、周五 11 号、周六 12 号

有什么想法吗?谢谢!

Here's the given input from the user:

Year = 2011, Month = 3 (March), Week = 2

I want to get the days in week 2 of March 2011 in JavaScript.

e.g. Sunday 6th, Monday 7th, Tuesday 8th, Wednesday 9th, Thursday 10th, Friday 11th, Saturday 12th

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人 2024-12-01 15:57:31

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

dateNumbersOfMonthOnWeek

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

}

给定然后

  • 将获得该周的日期数字。
  • datesOfMonthOnWeek 将包含该周的日期对象。

虽然这对于这项工作来说似乎有点大材小用,但要使其在所有情况下都有效,例如当日期数字跨越到另一个月份时,需要做很多工作。不过,我确信它可以优化得不那么冗长。

Given

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

and

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

}

then

  • dateNumbersOfMonthOnWeek will have the date numbers of that week.
  • datesOfMonthOnWeek will have the date objects of that week.

While this may seem like it is overkill for the job, much of this is required to make it work in all situations, like when the date numbers cross over to another month. I'm sure it could be optimised to be less verbose, though.

感性不性感 2024-12-01 15:57:31

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

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

I normally use Datejs for date manipulations in js. I'd do it like this:

var firstDayOfWeek = new Date(year,month-1,1).moveToDayOfWeek(0,-1).addWeeks(week-1);
var lastDayOfWeek = firstDayOfWeek.addDays(6);
谁把谁当真 2024-12-01 15:57:31

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

/* 
   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次,每次在日期上加1,例如

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

Assuming that the first week of the month is the one with the first of the month in it, then the following function will return the date of the first day of the week given year, month and week number:

/* 
   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

To get the rest of the days, just loop 6 times adding one to the date each time, e.g.

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]);
}
等待我真够勒 2024-12-01 15:57:31

对第一个对我有用的答案进行一些修改:

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
}

A little modification of the first answer that worked for me:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文