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());
  • can you use the excellent datetime javascript library momentjs? – Paul Lucas Mar 4 '16 at 9:55
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));
  • Thank You Ankur... can you pls make it startdate as sunday and endate as saturday in a week – Lavanya Puppala Mar 4 '16 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 :) – Ankur Hambir Mar 4 '16 at 11:25
  • Lavanya, Is your problem solved ?? – Ankur Hambir Mar 4 '16 at 13:11
  • Most Welcome :) – Ankur Hambir Mar 4 '16 at 13:16
  • Hi Ankur, this is not working for normal weeks.. That means week starts and ends in same month – Lavanya Puppala Mar 9 '16 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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