[英]Get the dates of the week in javascript
I would like to get the first day and last day in the week using JavaScript. 我想使用JavaScript获取一周中的第一天和最后一天。
For example, today is 2016-13-12. 例如,今天是2016-13-12。
FD = 2016-11-12 LD = 2016-17-12 FD = 2016-11-12 LD = 2016-17-12
How can I get the FD and LD? 如何获得FD和LD? I tried using this code, 我尝试使用此代码,
Date.prototype.GetFirstDayOfWeek = function() {
return (new Date(this.setDate(this.getDate() - this.getDay())));
}
Date.prototype.GetLastDayOfWeek = function() {
return (new Date(this.setDate(this.getDate() - this.getDay() +6)));
}
var today = new Date();
It is working but I want to format it in "2016-12-11". 它正在工作,但我想在“ 2016-12-11”中设置其格式。 Also I would like also to change the date into a past or future. 我也想将日期更改为过去或将来。 For example, If I click the button "<" it will go past 1 week. 例如,如果我单击按钮“ <”,它将超过1周。 then if the user click again the button, it will get the next last week. 然后,如果用户再次单击该按钮,它将获得下一个上周。 and so on. 等等。
Scenario: DAtes now is 2016-11-12 - 2016-17-12 When the user click button "<". 场景:日期现在为2016-11-12-2016-17-12当用户单击按钮“ <”时。 it will become 2016-04-12 - 2016-10-12 它将成为2016-04-12-2016-10-12
You can use script below to find firstDay and lastDay of current week; 您可以使用以下脚本查找当前星期的firstDay和lastDay;
//0=Sunday, 1=Monday
var d = new Date();
var day = d.getDay();
var firstDayOW = d.getDate() - day + (day == 0 ? -6:1);
firstDay = new Date(d.setDate(firstDay));
var lastDayOW = firstDay + 6;
lastDayOW = new Date(d.setDate(lastDayOW));
I hope this helps you. 我希望这可以帮助你。
Kind regards. 亲切的问候。
var curr = new Date; // 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)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.