2012-03-23 170 views
3

在我的表单中我有一个数据字段,用于选择星期几! 例如,如果我今天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] 

我如何能做到这一点的明显关注的变化每周的任何一天选择? 感谢

+0

如果选定日期是星期一? – 2012-03-23 13:29:14

回答

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

很好,先生!但是,我会使用date.getFullYear(),否则一周中的某一天将不正确。 – Dom 2012-10-11 15:26:10

1

mayby尝试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) 
0

用于显示的一周中的当前天:

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

我认为OP本周需要所有的日子,而不仅仅是今天。 – 2012-03-23 13:08:58

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; 
+0

星期日的错误。 – 2012-03-23 13:12:43

+0

@肯德尔弗雷请详细说明一下吗? – Curt 2012-03-23 13:13:36

+0

周日将返回前一周,而不是当前周。 – 2012-03-23 13:15:54

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

*/ 
相关问题