计算星期一到星期六的星期几

时间:2012-03-23 12:51:00

标签: javascript

在我的表格中,我有一个数据字段,我可以选择星期几! 例如,如果我今天选择23-03-2012星期五,我需要从上周一到下周六获得一系列日子。

数组:

[0],[19-03-2012],[Monday]
[1],[20-03-2012],[Monday]
[2],[21-03-2012],[Wednesday]
[3],[22-03-2012],[Monday]
[4],[23-03-2012],[Friday]
[5],[24-03-2012],[Saturday]

我如何在一周的任何选定日期明确注意变化? 感谢

6 个答案:

答案 0 :(得分:2)

此函数将返回星期一至星期六date周内所有日期的数组。

function GetDaysOfWeek(date)
{
    var days = new Array();
    for (var i = 0; i < 6; i++)
    {
        days[i] = new Date(date.getYear(),
                           date.getMonth(),
                           date.getDate() - date.getDay() + 1 + i);
    }
    return days;
}

答案 1 :(得分:1)

可能会尝试MomentJs:http://momentjs.com/docs/

一些例子:

moment().day(-7); // set to last Sunday (0 - 7) 
moment().day(7); // set to next Sunday (0 + 7)
moment().day(10); // set to next Wednesday (3 + 7)
moment().day(24); // set to 3 Wednesdays from now (3 + 7 + 7 + 7)

答案 2 :(得分:0)

显示当天的当天:

 var now = new Date();
 var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
 document.write("Today is " + dayNames[now.getDay()] + ".");

答案 3 :(得分:0)

  • 首先找到今天的日期
  • 查找最后一个星期一(包括今天)
  • 显示该日期及其后5天(周二至周六)

var d = new Date();

if (d.getDay()==0){
   d.setDate(d.getDate() + 1);
}

​while (d.getDay() != 1){
    d.setDate(d.getDate() - 1);
}

var days = new Array();

for (var i = 0; i < 6; i++){
  days[i] = d.getDate() + i;
}

return days;

答案 4 :(得分:0)

试试这个:

var dayString = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

var now = new Date();
var currentDay = now.getDay(); // return 0 for Sunday, 6 for Saturday
var result = [];
var tempDate = new Date(now.getTime());
tempDate.setDate(now.getDate()-(currentDay+6)%7); // now tempDate is previous Monday
while(tempDate.getDay()!=0) {
    var currentMonth = tempDate.getMonth()+1;
    if(currentMonth<10) currentMonth = "0"+currentMonth;
    result.push([tempDate.getDay()-1,tempDate.getDate()+"-"+currentMonth+"-"+tempDate.getFullYear(),dayString[tempDate.getDay()]]);
    tempDate.setDate(tempDate.getDate()+1);
}
console.log(result);

答案 5 :(得分:0)

以下内容可以解决问题,我确信你可以将格式化到你想要的位置。

// Assuming d is a date object
function getDateArray(din) {

  // Add leading zero to one digit numbers
  function aZ(n){return (n<10? '0':'') + n;}

  var days = ['Sunday','Monday','Tuesday','Wednesday',
              'Thursday','Friday','Saturday'];
  var d = new Date(din); // Don't wreck input date  
  var dn = d.getDay();
  var a = [];
  var i = 6; // length of day array

  if (!dn) {
    // It's Sunday, what now? 
    return ['Sunday!'];
  }

  d.setDate(d.getDate() + 6 - dn); // Next Saturday

  do {
    a[i--] = i + ' ' + aZ(d.getDate()) +
             '-' + aZ(d.getMonth() + 1) +
             '-' + d.getFullYear() + 
             ' ' + days[d.getDay()];     
    d.setDate(d.getDate() - 1);
  } while (i);

  return a;
}

// Test it
var date = new Date(2012,2,2)

alert( date + '\n\n' + getDateArray(date).join('\n'));

/*

  Fri Mar 02 2012 00:00:00

  0 27-02-2012 Monday
  1 28-02-2012 Tuesday
  2 29-02-2012 Wednesday
  3 01-03-2012 Thursday
  4 02-03-2012 Friday
  5 03-03-2012 Saturday

*/