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 = newDate(this.setHours(0, 0, 0, 0));
var day = today.getDay() - start;
var date = today.getDate() - day;
// Grabbing Start/End DatesvarStartDate = newDate(today.setDate(date));
varEndDate = newDate(today.setDate(date + 6));
return [StartDate, EndDate];
}
// test codevarDates = newDate().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 datefunctionstartAndEndOfWeek(date) {
// If no date object supplied, use current date// Copy date so don't modify supplied datevar now = date? newDate(date) : newDate();
// set time to some convenient value
now.setHours(0,0,0,0);
// Get the previous Mondayvar monday = newDate(now);
monday.setDate(monday.getDate() - monday.getDay() + 1);
// Get next Sundayvar sunday = newDate(now);
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
// Return array of date objectsreturn [monday, sunday];
}
// Mon Nov 12 2012 00:00:00// Sun Nov 18 2012 00:00:00alert(startAndEndOfWeek(newDate(2012,10,14)).join('\n'));
Solution 3:
Try this:
var current = newDate(); // get current date var weekstart = current.getDate() - current.getDay() +1;
var weekend = weekstart + 6; // end day is the first day + 6 var monday = newDate(current.setDate(weekstart));
var sunday = newDate(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 = newDate();
todayNumber = today.getDay();
mondayNumber = 1 - todayNumber;
sundayNumber = 7 - todayNumber;
monday = newDate(today.getFullYear(), today.getMonth(), today.getDate()+mondayNumber);
sunday = newDate(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 =newDate("08-Jul-2014"); //getcurrentdate
var first= curr.getDate() - curr.getDay(); //Firstdayis the dayof the month- the dayof the week
var last=first+6; //lastdayis the firstday+6
var firstday =newDate(curr.setDate(first)); //06-Jul-2014
var lastday =newDate(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 = newDate();
day = curr.getDay();
firstday = newDate(curr.getTime() - 60*60*24* day*1000); // will return firstday (i.e. Sunday) of the week
lastday = newDate(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 )"