Get start date and end date of current week (week

2020-02-25 04:53发布

I want to display start date and end date of current week. (week start=monday, week end=sunday) I manage to display monday.

But I'm unable to get sunday date. Please post me if anyone has solution.

11条回答
够拽才男人
2楼-- · 2020-02-25 05:28

Slightly Modified answer of @ReadWriteCode, this also works successfully with different months.

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();
查看更多
放我归山
3楼-- · 2020-02-25 05:31

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.

查看更多
走好不送
4楼-- · 2020-02-25 05:33

SetDate will sets the day of the month. Using setDate during start and end of the month, will result in wrong week

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(curr.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

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(curr.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
查看更多
【Aperson】
5楼-- · 2020-02-25 05:34
  1. Different months issues is also fixed with below code.
  2. Added format "M.dd.yyyy" function.

    function startAndEndOfWeek(setDate) { var day = null; var date = null;var month = null;var year = null; var mon = null; var tue = null; var wed = null; var thu = null; var fri = null; var sat = null; var sun = null;

    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];
    

    }

    function getMonthValue(month) { switch(month) { case 0: return "Jan"; break; case 1: return "Feb"; break; case 2: return "Mar"; break; case 3: return "Apr"; break; case 4: return "May"; break; case 5: return "Jun"; break; case 6: return "Jul"; break; case 7: return "Aug"; break; case 8: return "Sep"; break; case 9: return "Oct"; break; case 10: return "Nov"; break; case 11: return "Dec"; break; } } function getDateValue(day) { if(parseInt(day) < 10) { return "0" + day; } return day; }

查看更多
Root(大扎)
6楼-- · 2020-02-25 05:37

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));
查看更多
smile是对你的礼貌
7楼-- · 2020-02-25 05:41

Using https://momentjs.com makes it more simple.

  1. To get week starting with Sunday and ending with Saturday, then use: moment().startOf("week").toDate() and moment().endOf("week").toDate()

  2. To get week starting with Monday and ending with Sunday, then use: moment().startOf("isoWeek").toDate() and moment().endOf("isoWeek").toDate()

Tried this on moment v2.18.1, it works.

Hope this helps, for more you can refer moment docs.

查看更多
登录 后发表回答