Javascript 获取当前周的开始日期和结束日期(从周一开始到周日结束的一周)
我想显示本周的开始日期和结束日期。(周开始=周一,周结束=周日) 我设法在星期一展示 但我无法得到周日的约会。 如果有人有解决办法,请发邮件给我Javascript 获取当前周的开始日期和结束日期(从周一开始到周日结束的一周),javascript,date,Javascript,Date,我想显示本周的开始日期和结束日期。(周开始=周一,周结束=周日) 我设法在星期一展示 但我无法得到周日的约会。 如果有人有解决办法,请发邮件给我 <script> Date.prototype.getWeek = function(start) { //Calcing the starting point start = start || 0; var today = new Date(this.setHours(0, 0, 0, 0));
<script>
Date.prototype.getWeek = function(start)
{
//Calcing the starting point
start = start || 0;
var today = new Date(this.setHours(0, 0, 0, 0));
var day = today.getDay() - start;
var date = today.getDate() - day;
// Grabbing Start/End Dates
var StartDate = new Date(today.setDate(date));
var EndDate = new Date(today.setDate(date + 6));
return [StartDate, EndDate];
}
// test code
var Dates = new Date().getWeek();
alert(Dates[0].toLocaleDateString() + ' to '+ Dates[1].toLocaleDateString())
</script>
Date.prototype.getWeek=函数(开始)
{
//煅烧起点
开始=开始| | 0;
var today=新日期(这个.setHours(0,0,0,0));
var day=today.getDay()-start;
var date=today.getDate()-day;
//抓取开始/结束日期
var StartDate=新日期(今天.设置日期(日期));
var EndDate=新日期(今天.setDate(日期+6));
返回[开始日期,结束日期];
}
//测试代码
变量日期=新日期().getWeek();
警报(日期[0]。toLocaleDateString()+'到“+日期[1]。toLocaleDateString())
我相信这是可行的。您可以修改返回值以满足您的需要,现在它只返回星期天的完整对象
Date.prototype.endWeek=function(){
return new Date(this.getFullYear(), this.getMonth(),(7-this.getDay())+this.getDate());
}
//call like this
var myDate = new Date();
var Sunday = myDate.endWeek();
alert("Sunday falls on "+ Sunday.getDate());
以下函数将执行此操作:
// return an array of date objects for start (monday)
// and end (sunday) of week based on supplied
// date object or current date
function startAndEndOfWeek(date) {
// If no date object supplied, use current date
// Copy date so don't modify supplied date
var now = date? new Date(date) : new Date();
// set time to some convenient value
now.setHours(0,0,0,0);
// Get the previous Monday
var monday = new Date(now);
monday.setDate(monday.getDate() - monday.getDay() + 1);
// Get next Sunday
var sunday = new Date(now);
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
// Return array of date objects
return [monday, sunday];
}
// Mon Nov 12 2012 00:00:00
// Sun Nov 18 2012 00:00:00
alert(startAndEndOfWeek(new Date(2012,10,14)).join('\n'));
试试这个:
var current = new Date(); // get current date
var weekstart = current.getDate() - current.getDay() +1;
var weekend = weekstart + 6; // end day is the first day + 6
var monday = new Date(current.setDate(weekstart));
var sunday = new Date(current.setDate(weekend));
SetDate
将设置月份的日期。在月初和月末使用setDate
,将导致错误的一周
var curr = new Date("08-Jul-2014"); // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)); // 06-Jul-2014
var lastday = new Date(firstday .setDate(last)); // 12-Jul-2014
如果设置日期为2014年7月1日,则第一天显示为2014年6月29日,最后一天显示为2014年6月5日,而不是2014年7月5日。所以为了克服这个问题,我使用了
var curr = new Date();
day = curr.getDay();
firstday = new Date(curr.getTime() - 60*60*24* day*1000); // will return firstday (i.e. Sunday) of the week
lastday = new Date(firstday.getTime() + 60 * 60 *24 * 6 * 1000); // adding (60*60*6*24*1000) means adding six days to the firstday which results in lastday (Saturday) of the week
在其他例子中,当星期日在其他月份时,您将遇到问题。 这应该可以解决问题:
var today, todayNumber, mondayNumber, sundayNumber, monday, sunday;
today = new Date();
todayNumber = today.getDay();
mondayNumber = 1 - todayNumber;
sundayNumber = 7 - todayNumber;
monday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+mondayNumber);
sunday = new Date(today.getFullYear(), today.getMonth(), today.getDate()+sundayNumber);
我希望这对你有用:
Date.prototype.getWeekEndDate = function () {
diff = 6 - this.getDay();
if (diff < 0) {
diff += 6;
}
this.setDate(this.getDate() + (1 * diff));
return this;
}
Date.prototype.getWeekStartDate = function () {
diff = this.getDay() - 6;
if (diff < 0) {
diff += 7;
}
return this.setDate(this.getDate() + (-1 * diff));
}
Date.prototype.getWeekEndDate=函数(){
diff=6-this.getDay();
如果(差异<0){
diff+=6;
}
this.setDate(this.getDate()+(1*diff));
归还这个;
}
Date.prototype.getWeekStartDate=函数(){
diff=this.getDay()-6;
如果(差异<0){
diff+=7;
}
返回此.setDate(此.getDate()+(-1*diff));
}
这些方法将返回一周的开始日期和结束日期
var now = setDate ? new Date(setDate) : new Date();
now.setHours(0,0,0,0);
var monday = new Date(now); var tuesday = new Date(now); var wednesday = new Date(now); var thursday = new Date(now);
var friday = new Date(now); var saturday = new Date(now); var sunday = new Date(now);
if(new Date(now).getDay() == 0)
{
monday.setDate(monday.getDate() - 6);
tuesday.setDate(tuesday.getDate() - 5);
wednesday.setDate(wednesday.getDate() - 4);
thursday.setDate(thursday.getDate() - 3);
friday.setDate(friday.getDate() - 2);
saturday.setDate(saturday.getDate() - 1);
sunday.setDate(sunday.getDate() - 0);
}
else
{
monday.setDate(monday.getDate() - monday.getDay() + 1);
tuesday.setDate(tuesday.getDate() - tuesday.getDay() + 2);
wednesday.setDate(wednesday.getDate() - wednesday.getDay() + 3);
thursday.setDate(thursday.getDate() - thursday.getDay() + 4);
friday.setDate(friday.getDate() - friday.getDay() + 5);
saturday.setDate(saturday.getDate() - saturday.getDay() + 6);
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
}
date = monday.getDate();
month = monday.getMonth();
year = monday.getFullYear();
mon = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = tuesday.getDate();
month = tuesday.getMonth();
year = tuesday.getFullYear();
tue = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = wednesday.getDate();
month = wednesday.getMonth();
year = wednesday.getFullYear();
wed = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = thursday.getDate();
month = thursday.getMonth();
year = thursday.getFullYear();
thu = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = friday.getDate();
month = friday.getMonth();
year = friday.getFullYear();
fri = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = saturday.getDate();
month = saturday.getMonth();
year = saturday.getFullYear();
sat = getMonthValue(month) + "." + getDateValue(date) + "." + year;
date = sunday.getDate();
month = sunday.getMonth();
year = sunday.getFullYear();
sun = getMonthValue(month) + "." + getDateValue(date) + "." + year;
return [mon, tue , wed, thu, fri, sat, sun];
}
函数getMonthValue(月)
{
开关(月)
{
案例0:返回“Jan”;
打破
案例1:返回“Feb”;
打破
案例2:返回“Mar”;
打破
案例3:返回“Apr”;
打破
案例4:返回“可能”;
打破
案例5:返回“Jun”;
打破
案例6:返回“Jul”;
打破
案例7:返回“Aug”;
打破
案例8:返回“Sep”;
打破
案例9:返回“Oct”;
打破
案例10:返回“Nov”;
打破
案例11:返回“Dec”;
打破
}
}
函数getDateValue(天)
{
如果(parseInt(天)<10)
{
返回“0”+天;
}
回归日;
}根据罗布的回答,我以这句话结束了本周,从周一午夜开始,到周日23:59:59.999结束
var weekMap = [6, 0, 1, 2, 3, 4, 5];
function startAndEndOfWeek(date) {
var now = new Date(date);
now.setHours(0, 0, 0, 0);
var monday = new Date(now);
monday.setDate(monday.getDate() - weekMap[monday.getDay()]);
var sunday = new Date(now);
sunday.setDate(sunday.getDate() - weekMap[sunday.getDay()] + 6);
sunday.setHours(23, 59, 59, 999);
return [monday, sunday];
}
稍微修改了@ReadWriteCode的答案,在不同月份也能成功使用
Date.prototype.getWeek = function()
{
var today = new Date();
var day = today.getDay();
var date = today.getDate() - day;
// Grabbing Start/End Dates
var StartDate = new Date();
var EndDate = new Date();
StartDate.setHours(0,0,0,0); EndDate.setHours(0,0,0,0);
StartDate.setDate(today.getDate()-day);
EndDate.setDate(today.getDate()-day+6);
return {
startDate:StartDate,
endDate: EndDate
};
}
var thisWeekDates=new Date().getWeek();
使用会使它更简单
您的“星期一”代码是什么?@user725184,如果您对答案感到满意,请接受答案。有关详细信息,请参阅。这有点令人讨厌,因为它不必要更改日期对象的时间和日期。这不适用于开始日期和结束日期在不同月份的情况。只需尝试今天打印开始日期和结束日期,您就会得到错误的答案。它返回8月30日作为开始日期,8月5日作为结束日期。这就是开始日期和结束日期相同的情况不同的月份,它给出了错误的答案。这里唯一一个处理所有特殊情况的解决方案,thanks@habib-你能解释一下它不起作用的日期吗?@Habib:因为今天2016年9月28日,它起作用很好,而本周是重叠的月份。完美的解决方案:)这一个非常适合所有场景,我们正在CouchDB列表函数中使用它来为带有时间戳的文档每周聚合总和,到目前为止,它似乎工作得相当好。
function endOfWeek(date)
{
var lastday = date.getDate() - (date.getDay() - 1) + 6;
return new Date(date.setDate(lastday));
}
dt = new Date();
console.log(endOfWeek(dt).toString());