1

As I am new to jquery and I need to display first and last sunday in my website.I got this code from stackoverflow but it enables all sundays so please help me

function enableSUNDAYS(date) {
var day = date.getDay();
return [(day == 0), ''];
}
Erik
  • 935
  • 11
  • 28
user3193751
  • 23
  • 1
  • 9

1 Answers1

1

The following should work;

function enableFirstAndLastSunday(date) {
    var lastDayInMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
    var isSunday = date.getDay() == 0;
    var isFirstDayOfMonth = date.getDate() <= 7;
    var isLastDayOfMonth = date.getDate() > lastDayInMonth - 7;

    return [isSunday && (isFirstDayOfMonth || isLastDayOfMonth), '']; 
}

It checks that the date is a Sunday (using the same code you provided), but also checks that it occurs within either the first 7 days of the month, or the last 7. The calculation of lastDayInMonth is based on this hack.

You can see a demo of this working here (thanks Salman A for the demo).

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180