2 Replies Latest reply on Sep 4, 2008 4:25 AM by MarkWalsh

    Auto filling the last day of month

      Hi all,

      I'm having a little problem here. I'm trying to get a form field to automatically fill out with the last day of the month. Could someone help me out with the javascript code for that. The problem I'm obviously having is that some months end with a 31 and some with a 30..and in rare cases with a 28 or 29! All I need is the last day of the month, not the month itself.

      Thanks for any help!
        • 1. Re: Auto filling the last day of month
          George_Johnson MVP & Adobe Community Professional
          Here are some functions that may help:

          function isLeapYear(year) {
             return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
          }

          function daysInMonth(month, year) {

             var leap = isLeapYear(year);

             switch (month) {

                  case (1): // Jan
                  case (3): // Mar
                  case (5): // May
                  case (7): // Jul
                  case ( 8 ): // Aug
                  case (10): // Oct
                  case (12): // Dec
                      return 31;
                  case (4): // Apr
                  case (6): // Jun
                  case (9): // Sep
                  case (11): // Nov
                      return 30;
                  case (2): // Feb
                      if (leap) return 29;
                      else      return 28;
                  default:
                      return 0;
          }
          }

          The year parameter for the isLeapYear function is expected to be a number (e.g., 2008 vs 08) representing the year.

          The month parameter for the daysInMonth function is expected to be a number from 1 to 12.

          George
          • 2. Re: Auto filling the last day of month
            MarkWalsh Level 4
            The easiest way to calculate the last day of a month is to calculate the day before the first day of the next month.