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.
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.
Slightly Modified answer of @ReadWriteCode, this also works successfully with different months.
I hope this will work for you :
These method will return start date and end date of the week.
SetDate
will sets the day of the month. UsingsetDate
during start and end of the month, will result in wrong weekIf 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
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;
}
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; }
Try this:
Using https://momentjs.com makes it more simple.
To get week starting with Sunday and ending with Saturday, then use: moment().startOf("week").toDate() and moment().endOf("week").toDate()
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.