Skip to main content
Participating Frequently
May 4, 2016
Answered

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

  • May 4, 2016
  • 1 reply
  • 2535 views

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!

This topic has been closed for replies.
Correct answer gkaiseril

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 reply

gkaiserilCorrect answer
Inspiring
May 4, 2016

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;

}

msingsonAuthor
Participating Frequently
May 4, 2016

That did it for me! Thank you very much!