Javascript 计算一周中从周一到周六的天数
在我的表单中,我有一个数据字段,我在其中选择一周中的哪一天! 例如,如果我选择今天2012年3月23日星期五,我需要获取从前一个星期一到下一个星期六的天数数组Javascript 计算一周中从周一到周六的天数,javascript,Javascript,在我的表单中,我有一个数据字段,我在其中选择一周中的哪一天! 例如,如果我选择今天2012年3月23日星期五,我需要获取从前一个星期一到下一个星期六的天数数组 function GetDaysOfWeek(date) { var days = new Array(); for (var i = 0; i < 6; i++) { days[i] = new Date(date.getYear(),
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;
}
数组:
[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]
我怎样才能在一周中的任何一天都注意到变化?
谢谢mayby试用MomentJs:
一些例子:
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)
要显示一周中的当前日期,请执行以下操作:
var now = new Date();
var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
document.write("Today is " + dayNames[now.getDay()] + ".");
此函数将返回
日期
一周中的所有日期的数组,即周一到周六
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;
}
函数GetDaysOfWeek(日期)
{
var days=新数组();
对于(变量i=0;i<6;i++)
{
天[i]=新日期(Date.getYear(),
date.getMonth(),
date.getDate()-date.getDay()+1+i);
}
返程天数;
}
- 第一次找到今天的日期
- 查找最后一个星期一(包括今天)
- 显示该日期以及之后的5天(星期二和星期六)
var d=新日期();
如果(d.getDay()==0){
d、 设置日期(d.getDate()+1);
}
而(d.getDay()!=1){
d、 setDate(d.getDate()-1);
}
var days=新数组();
对于(变量i=0;i<6;i++){
天[i]=d.getDate()+i;
}
返程天数;
试试这个:
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);
var dayString=[“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”];
var now=新日期();
var currentDay=now.getDay();//周日返回0,周六返回6
var结果=[];
var tempDate=新日期(now.getTime());
tempDate.setDate(现在是.getDate()-(currentDay+6)%7);//现在的日期是前一个星期一
while(tempDate.getDay()!=0){
var currentMonth=tempDate.getMonth()+1;
如果(currentMonth类似于以下内容的内容可以实现此目的,我相信您可以将格式设置到所需的位置
// 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
*/
//假设d是日期对象
函数getDateArray(din){
//将前导零与一位数相加
函数aZ(n){return(我认为OP需要本周所有的日子,而不仅仅是今天。@KendallFrey请详细说明?一个星期将返回前一周,而不是本周。@KendallFrey干杯我认为现在已经修复了看起来不错。我不知道您是否注意到,但严格来说,if
块不是必需的。;)这样可能更容易阅读,也没什么大不了的。我只是想提一下。:DNicely done,sir!但是,我会使用date.getFullYear(),否则一周中的哪一天就不正确了。