简体   繁体   English

在javascript中获取一周的前几天和后几天

[英]Get the previous and next days of the week in javascript

I have this code that get the current date, then get also the days of the week.我有这个获取当前日期的代码,然后还获取一周中的几天。 Sometimes this code is not working, for example, today is 08/15, I will get 08/13.有时这段代码不起作用,例如,今天是 08/15,我会得到 08/13。 I need also to have a function get the previous and next days of the week.我还需要有一个函数来获取一周的前几天和后几天。

var self = this;
var curr = new Date;
self.mult = 0;
    self.firstDate = new Date;
    self.lastDate = new Date;
    var first = curr.getDate() - (curr.getDay()-1);
    var last = first + 6; 
    self.firstDate(Utils.dateFormat(new Date(curr.setDate(first)),"%Y-%m-%d", true));
    self.lastDate(Utils.dateFormat(new Date(curr.setDate(last)),"%Y-%m-%d", true));

And this is the function to see previous days of the week,这是查看一周前几天的功能,

var self= this;
    var curr = new Date;
    self.mult(self.mult()-7);
    var first = curr.getDate() - (curr.getDay()-1) + self.mult();
    var last = first + 5;
    if(self.mult() <= 0){
        last = last + 1;
    }
    self.firstDate(Utils.dateFormat(new Date(curr.setDate(first)),"%Y-%m-%d", true));
    self.lastDate(Utils.dateFormat(new Date(curr.setDate(last)),"%Y-%m-%d", true));

And this is the function to see the next days of the week,这是查看一周中接下来几天的功能,

var self= this;
    var curr = new Date;
    self.mult(self.mult()+7);
    var first = curr.getDate() - (curr.getDay()-1) + self.mult();
    var last = first + 5;
    self.firstDate(Utils.dateFormat(new Date(curr.setDate(first)),"%Y-%m-%d", true));
    self.lastDate(Utils.dateFormat(new Date(curr.setDate(last)),"%Y-%m-%d", true));

I am not sure but I think only previous days of the week is the problem.我不确定,但我认为只有一周的前几天是问题所在。

Here is a sample situation.这是一个示例情况。

Scenario 1: User visit a page, the today is 09/15 then the firstDate must be 09/12 then lastDate must be 09/18 the user click next, then the firstDate must be 09/19 and the lastDate is 09/25 the user click next again, then the firstDate is 09/26 and the lastDate is 10/02 the user click previous, then the firstDate must be 09/19 and the lastDate is 09/25场景1:用户访问页面,今天是09/15然后firstDate必须是09/12然后lastDate必须是09/18用户点击next,那么firstDate必须是09/19并且lastDate是09/25用户再次单击下一步,则 firstDate 为 09/26,lastDate 为 10/02 用户单击上一个,则 firstDate 必须为 09/19,lastDate 为 09/25

Scenario 2: User visit a page, the today is 09/15 then the firstDate must be 09/12 then lastDate must be 09/18 the user click previous, then the firstDate must be 09/5 and the lastDate is 09/11 the user click previous again, then the firstDate must be 08/29 and the lastDate is 09/4 the user click next, then the firstDate must be 09/5 and the lastDate is 09/11场景 2:用户访问页面,今天是 09/15 那么 firstDate 必须是 09/12 然后 lastDate 必须是 09/18 用户点击previous,那么 firstDate 必须是 09/5 并且 lastDate 是 09/11用户再次单击上一个,则 firstDate 必须为 08/29,lastDate 为 09/4 用户单击 next,则 firstDate 必须为 09/5,lastDate 为 09/11

Can someone answer a code that can do my scenarios?有人可以回答可以完成我的场景的代码吗?

You can use setDate()您可以使用setDate()

for example:例如:

var a = new Date();
console.log(a)
//Thu Sep 15 2016 10:22:25 GMT+0200 (CEST)
a.setDate(a.getDate()+1);
console.log(a)
//Fri Sep 16 2016 10:22:25 GMT+0200 (CEST)

I'd recommend use library for tasks like this.我建议将库用于此类任务。 For example moment.js.例如moment.js。

Start of week from now从现在开始一周

moment(new Date()).startOf('isoWeek').format('MMMM Do YYYY, h:mm:ss ')

End of week from now从现在开始的周末

moment(new Date()).endOf('isoWeek').format('MMMM Do YYYY, h:mm:ss ')

For more details please see: http://momentjs.com/更多详情请见: http : //momentjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM