简体   繁体   中英

How I can get the date this week between Monday and Sunday AngularJS

How I can get the date this week between Monday and Sunday

Example the date between , this week is : Monday = 23/11/2015 to Sunday 29/11/2015 I want to get dates every week, between Monday and Sunday

AngujarJS , JavaScript

In vanilla you could set the day of the week on a Date instance like this,

function setDay(d, day, week_starts_monday) {
    var c_day = d.getDay();
    if (week_starts_monday && c_day === 0)
        c_day = 7; // adjust so `0` is last week sunday, 7 is this week sunday
    d.setDate(d.getDate() - c_day + day);
    return d;
}

So, the Monday of this week would be (today being Friday 27th)

var d = new Date;
setDay(d, 1); // Mon Nov 23 2015 18:51:45 GMT+0000 (GMT Standard Time)

7 is the next Sunday . 0 is the previous Sunday (or the current day if it is Sunday).

setDay(d, 7); // Sun Nov 29 2015 18:51:45 GMT+0000 (GMT Standard Time)

You can now iterate as desired with for ,

var i, d = new Date;
for (i = 1; i <= 7; ++i) {
    setDay(d, i);
    // do something with `d`
}

Please note

  • Crossing timezones may cause unexpected results. Maybe you'll want to do this in UTC?
  • If you wish to go backwards from Sunday you will need to use negative numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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