Javascript 获取本周的开始日期和结束日期(一周从星期一开始,以星期日结束)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8381427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get start date and end date of current week (week start from monday and end with sunday )
提问by Vishnu
I want to display start date and end date of current week. (week start=monday, week end=sunday) I manage to display monday.
我想显示本周的开始日期和结束日期。(week start=monday, week end=sunday) 我设法显示星期一。
But I'm unable to get sunday date. Please post me if anyone has solution.
但我无法得到星期天的约会。如果有人有解决方案,请张贴我。
回答by ReadWriteCode
<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>
I believe this works.
我相信这有效。
回答by RobG
The following function will do the trick:
以下函数可以解决问题:
// 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'));
回答by Rawat Raman
Try this:
尝试这个:
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));
回答by Orchid
SetDate
will sets the day of the month. Using setDate
during start and end of the month, will result in wrong week
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
If setting Date is 01-Jul-2014, it will show firstday as 29-Jun-2014 and lastday as 05-Jun-2014 instead of 05-Jul-2014. So to overcome this issue I used
如果设置 Date 为 01-Jul-2014,则将第一天显示为 29-Jun-2014,最后一天显示为 05-Jun-2014 而不是 05-Jul-2014。所以为了克服这个问题,我使用了
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
回答by Lars de Weert
In the other examples you will have a problem when sunday falls in other month. This should solve the problem:
在其他示例中,当星期日落在其他月份时,您会遇到问题。这应该可以解决问题:
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);
回答by jo2
Based on RobG's answer, I ended up with this to get week to start on midnight Monday and end on Sunday 23:59:59.999.
根据 RobG 的回答,我最终得到了这一周,从星期一午夜开始,到星期日 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];
}
回答by Rajat Sawant
Slightly Modified answer of @ReadWriteCode, this also works successfully with different months.
@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();
回答by Thanmai C
Using https://momentjs.commakes it more simple.
使用https://momentjs.com使其更简单。
To get week starting with Sunday and ending with Saturday, then use: moment().startOf("week").toDate() and moment().endOf("week").toDate()
To get week starting with Monday and ending with Sunday, then use: moment().startOf("isoWeek").toDate() and moment().endOf("isoWeek").toDate()
要获得从 Sunday 开始并以 Friday 结束的周,请使用: moment().startOf("week").toDate() 和 moment().endOf("week").toDate()
要获得从 Monday 开始到 Sunday 结束的一周,然后使用: moment().startOf("isoWeek").toDate() 和 moment().endOf("isoWeek").toDate()
Tried this on moment v2.18.1, it works.
在moment v2.18.1 上试过这个,它有效。
Hope this helps, for more you can refer moment docs.
希望这会有所帮助,有关更多信息,您可以参考 moment 文档。
回答by Hamed Zakery Miab
回答by Shivam Chopra
I hope this will work for you :
我希望这对你有用:
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));
}
These method will return start date and end date of the week.
这些方法将返回一周的开始日期和结束日期。