Skip to content Skip to sidebar Skip to footer

Get Start Date And End Date Of Current Week (week Start From Monday And End With Sunday )

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 an

Solution 1:

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


Solution 2:

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'));

Solution 3:

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

Solution 4:

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

Solution 5:

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

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

Post a Comment for "Get Start Date And End Date Of Current Week (week Start From Monday And End With Sunday )"