• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 04, 2016 May 04, 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 cus

...

Votes

Translate

Translate
LEGEND ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

That did it for me! Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 16, 2021 Aug 16, 2021

Copy link to clipboard

Copied

LATEST

Hey!

 

I'm trying to use this code, and sometimes it's working perfectly, but sometimes give "undefined" result for the same date value. I simply can't figure out why.

Do you have any idea why could cause my problem?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines