给定一个日期对象,如何获得前一周的第一天
答案 0 :(得分:6)
这Datejs library看起来相对容易做到这一点。
答案 1 :(得分:5)
代码:
function getPreviousSunday()
{
var today=new Date();
return new Date().setDate(today.getDate()-today.getDay()-7);
}
function getPreviousMonday()
{
var today=new Date();
if(today.getDay() != 0)
return new Date().setDate(today.getDate()-7-6);
else
return new Date().setDate(today.getDate()-today.getDay()-6);
}
<强>推理:强>
取决于前一周的第一天你的意思。为了讨论,我会假设你的意思是前一个星期天。
要查找要减去的天数:
...
确定减去天数后的实际代码很简单:
var previous_first_day_of_week=new Date().setDate(today.getDate()-X);
其中X是上面讨论的值。这个值是today.getDay()+ 7
如果在本周的第一天你有其他意思,你应该能够从上述步骤中推断出答案。
注意:将负值传递给setDate函数是有效的,它将正常工作。
关于星期一的代码。你有这种特殊情况,因为getDay()在星期一之前订购。因此,在这种情况下,我们基本上用getDay()的星期六值+ 1来替换getDay(),以便将星期日重新订购到本周末。
我们使用6的值作为星期一的减法因为getDay()每天返回1比我们想要的更高。
答案 2 :(得分:2)
function previousWeekSunday(d) {
return new Date(d.getFullYear(), d.getMonth(), d.getDate() - d.getDay() - 7);
}
function previousWeekMonday(d) {
if(!d.getDay())
return new Date(d.getFullYear(), d.getMonth(), d.getDate() - 13);
return new Date(d.getFullYear(), d.getMonth(), d.getDate() - d.getDay() - 6);
}
答案 3 :(得分:2)
我不太了解其他人的帖子。这是我用来显示相对于给定日期的周日周六的javascript。因此,举例来说,要获得“上周”,你要检查七天前相对于太阳/星期六球门柱的内容:new Date()-7
// variables
var comparedate = new Date()-7; // a week ago
var dayofweek = comparedate.getDay();
// just for declaration
var lastdate;
var firstadate;
// functions
function formatDate (dateinput) // makes date "mm/dd/yyyy" string
{
var month = dateinput.getMonth()+1;
if( month < 10 ) { month = '0' + month }
var date = dateinput.getDate();
if( date < 10 ) { var date = '0' + date }
var dateoutput = month + '/' + date + '/' + dateinput.getFullYear();
return dateoutput;
}
// Sunday to Saturday ... Sunday is the firstdate, Saturday is the lastdate
// (modify this block if you want something different eg: Monday to Sunday)
if ( dayofweek == 6 ) { lastdate = comparedate; firstdate = comparedate-6; } // Saturday
else if ( dayofweek == 0 ) { lastdate = comparedate+6; firstdate = comparedate; } // Sunday
else if ( dayofweek == 1 ) { lastdate = comparedate+5; firstdate = comparedate-1; } // Monday
else if ( dayofweek == 2 ) { lastdate = comparedate+4; firstdate = comparedate-2; } // Tuesday
else if ( dayofweek == 3 ) { lastdate = comparedate+3; firstdate = comparedate-3; } // Wednesday
else if ( dayofweek == 4 ) { lastdate = comparedate+2; firstdate = comparedate-4; } // Thursday
else if ( dayofweek == 5 ) { lastdate = comparedate+1; firstdate = comparedate-5; } // Friday
// Finish
var outputtowebpage = formatDate(firstdate) + ' - ' + formatDate(lastdate);
document.write(outputtowebpage);
每次我需要这样做时,我都要仔细查看。所以,我希望这对其他人有帮助。
答案 4 :(得分:1)
一周的第一天可以是周日或周一,具体取决于您所在的国家/地区:
function getPrevSunday(a) {
return new Date(a.getTime() - ( (7+a.getDay())*24*60*60*1000 ));
};
function getPrevMonday(a) {
return new Date(a.getTime() - ( (6+(a.getDay()||7))*24*60*60*1000 ));
};
如果您想将日期对象设置为上一个星期日,您可以使用:
a.setDate(a.getDate()-7-a.getDay());
以及之前的星期一:
a.setDate(a.getDate()-6-(a.getDay()||7));
答案 5 :(得分:0)
在其他例子中,当星期天在其他月份下降时,你会遇到问题。这应该可以解决问题:
var today, todayNumber, previousWeek, week, mondayNumber, monday;
today = new Date();
todayNumber = today.getDay();
previousWeek = -1; //For every week you want to go back the past fill in a lower number.
week = previousWeek * 7;
mondayNumber = 1 - todayNumber + week;
monday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+mondayNumber);