2 Replies Latest reply on May 4, 2016 9:31 AM by msingson

    Get number of days in a month based on month and year fields

    msingson Level 1

      I have a column on my form that enumerates the days in a month. I want to setup a hidden field that calculates the total number of days in a month based on month and year field inputs. The number of days will determine what shows up on the column. For example, if I put 4 in the month field, and 2016 in the year field, I get 30 in the hidden field. So on the "Day" column, I will have numbers 1 to 30 listed. Or if I put 2 in the month field and 2016 in the year field, I get 29 in the hidden field. So the numbers 1 - 29 will be listed in the "Day" column.

       

      Found this code from some javascript forum:

       

      //Month is 1 based
      function daysInMonth(month,year) {
        
      return new Date(year, month, 0).getDate();
      }

      //July
      daysInMonth
      (7,2009); //31
      //February
      daysInMonth
      (2,2009); //28
      daysInMonth
      (2,2008); //29

       

      I don't know how to convert this code to adobe JavaScript and don't really know how to use it. All I know how to do is to setup the field values for the month and year fields as variables. I am a novice programmer and would really appreciate all the help I can get. Thank you in advance!

        • 1. Re: Get number of days in a month based on month and year fields
          gkaiseril MVP & Adobe Community Professional

          The code appears to be JavaScript and runs as needed using the JavaScript console.

           

          I would consider making the code more general so if one has any date string that includes at least the month and year one could just call the function and get the number of days for that month.

           

          The following script will compute the number of days in a month using at a minimum the year and month values can display the result on the JavaScript console and set the field value for the field that has this code as the custom Calculation Script.

           

          function daysInMonth(oDate) {
             return new Date(oDate.getFullYear(), oDate.getMonth() + 1, 0).getDate();
          }

          var nMonth = this.getField("Month").valueAsString; // get month value;
          var nYear = this.getField("Year").valueAsString; // get year value;

          event.value = "";

          if(nMonth != "" && nYear != "") {
          var MyDate = util.scand("mm/yyyy", nMonth + "/" + nYear); // convert to date object;
          var nDaysInMonth = daysInMonth(MyDate); // get the number of days;

          console.open(); // open the JavaScript console;

          console.clear(); // clear the console;

          console.println("Days in " + nMonth + ": " + nDaysInMonth); // display the days in the month;

          event.value = nDaysInMonth; // set the field value;

          }

          1 person found this helpful
          • 2. Re: Get number of days in a month based on month and year fields
            msingson Level 1

            That did it for me! Thank you very much!