1

I have to get start date and and end date of current week. I used this code but end date is getting previous month name if week start in one month and ends in another month.

Date.prototype.getWeek = function (start) {

   start = start || 0;

   var today = new Date(this.setHours(0, 0, 0, 0));

   var day = today.getDay() - start;

   var date = today.getDate() - day;

   var StartDate = new Date(today.setDate(date));

   var EndDate = new Date(today.setDate(date + 6));

    return [StartDate, EndDate];
}

var Dates = new Date().getWeek();

  alert(Dates[0].toLocaleDateString() + ' to ' + Dates[1].toLocaleDateString());
1
  • can you use the excellent datetime javascript library momentjs?
    – Paul Lucas
    Mar 4, 2016 at 9:55

1 Answer 1

4

You can try below simple javascript code:

var mondate = new Date();
var sundate = new Date();
var monday = 0;
var sunday = 6;

var mondayDate = mondate.setDate(monday);
var sundayDate = sundate.setDate(sunday);

alert("Monday date is "+new Date(mondayDate)+" and Sunday date is "+new Date(sundayDate));
5
  • Thank You Ankur... can you pls make it startdate as sunday and endate as saturday in a week Mar 4, 2016 at 10:54
  • set variable monday = -1 (for the previous sunday) and set sunday = 5(for coming saturday), rename variable monday and sunday as Sunday and saturday respectively for better understanding. Hope this work for you :) Mar 4, 2016 at 11:25
  • Lavanya, Is your problem solved ?? Mar 4, 2016 at 13:11
  • Most Welcome :) Mar 4, 2016 at 13:16
  • Hi Ankur, this is not working for normal weeks.. That means week starts and ends in same month Mar 9, 2016 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.