Expand my Community achievements bar.

SOLVED

Number of days in a month

Avatar

Former Community Member

I am creating a form with 2 date objects:

"Date1" is a date object in which the user will enter the first day of a given month "MM/DD/YYYY";

I need "Date2" to calculate the last day of the month from "Date1" "MM/DD/YYYY".

Obviously, the date displayed in Date2 will vary because the months have a different number of days, so I can't just say "+30". Can someone help me with a JavaScript for FomCalc code to accomplish this?

thanks!!!!

Gene-O

1 Accepted Solution

Avatar

Correct answer by
Level 10

Maybe you're display pattern is different to the pattern in the script.

Here's a working example form:

https://acrobat.com/#d=rqvjWvn5WIBrWD*GLDRgfA

View solution in original post

12 Replies

Avatar

Level 10

Hi Gene-O,

In JavaScript you could put the following code in the calculate event of Date2;

if (Date1.isNull)
{
   null; // If they blank out Date1 blank out Date2
}
else
{
   var d = util.scand("yyyy-mm-dd", Date1.rawValue);
   d.setMonth(d.getMonth()+1,0); // Add 1 month and subtract 1 day
   util.printd("yyyy-mm-dd", d);
}

The line "d.setMonth(d.getMonth()+1,0);" does calculation, adds 1 month and the second parameter (the "0") is the number of days, using zero effectively subtracts one day.

It took me a while to get used to passing the result of a calculation event back, it takes the result of the last expression executed, but doesn't look right from a JavaScript point of view so you might like;

if (Date1.isNull)
{
   this.rawValue = null; // If they blank out Date1 blank out Date2
}
else
{
   var d = util.scand("yyyy-mm-dd", Date1.rawValue);
   d.setMonth(d.getMonth()+1,0); // Add 1 month and subtract 1 day
   this.rawValue = util.printd("yyyy-mm-dd", d);
}

This explicitly assigned the result.

Regards

Bruce

Avatar

Former Community Member

Bruce,

Sorry for the late reply, but I didn't realize you had helped me. Your script is great - thanks again!!!

Maybe you can help me again:

I am creating a time sheet with 2 pay periods for each month. The first pay period is ALWAYS the 1st-15th of the month. The second pay period is the 16th through the last day of the month (different for each month).

Can you help me with a script that will fill in the dates for the pay period?

Thanks so much!

Gene-O

Avatar

Level 10

Hi Gene-O

Try this script;

var today = new Date()

var period1Start = new Date(today.getFullYear(), today.getMonth(), 1)

var period1End = new Date(today.getFullYear(), today.getMonth(), 15)

var period2Start = new Date(today.getFullYear(), today.getMonth(), 16)

var period2End = new Date(today.getFullYear(), today.getMonth()+1, 0)

Regards

Bruce

Avatar

Former Community Member

Ok Bruce,

I think I understand what this script does, but I have no idea what to do with it. Stupid question: where do I put it to display the desired results?

thanks so much

Gene-O

Avatar

Level 10

Hi Gene-O,

I'm assuming your form will have four "calculated - read only" datetime fields. 

In the calculate event of the first one (to show the first day of the month) put;

var today = new Date();

util.printd("yyyy-mm-dd", new Date(today.getFullYear(), today.getMonth(), 1));

In the calculate event of the second one (to show the 15th day of the month) put;

var today = new Date();

util.printd("yyyy-mm-dd", new Date(today.getFullYear(), today.getMonth(), 15));

In the calculate event of the third one (to show the 16th day of the month) put;

var today = new Date();

util.printd("yyyy-mm-dd", new Date(today.getFullYear(), today.getMonth(), 16));

 

In the calculate event of the last one (to show the last day of the month) put;

var today = new Date();

util.printd("yyyy-mm-dd", new Date(today.getFullYear(), today.getMonth()+1, 0));

Good luck

Bruce

Avatar

Former Community Member

Bruce,

This is awesome, BUT, this grabs the current month. Sometimes I will have people that fill in the form for a previous month.

I will have the user enter the first day of the month for which they will fill out the timesheet. I need the form to calulcate the end date of THAT month, based on the user input. (not necessarily the current month)

thanks for being patient with me!!!

Gene-O

Avatar

Former Community Member

Paul,

Maybe you would be clearer on what I am looking for if I send you the form. I’m sorry that I am still too new at this to make myself clear!!!!!

This form has 2 pages, one timesheet for each pay period in a given month. The first page works well because the days are fixed.

The second page is where I have an issue:

The pay period will always start with the 16th of the month (PPSD2) . The pay period will end with the last day of the month (PPED2). This will obviously change from month to month. I need PPED2 to display the last day of the month based on the number of days in the month I am working on – NOT THE CURRENT MONTH.

Also, I need the second page to fill in the day number in the table (Table1.Row14.Date14) that corresponds with the day of the month.

For example: if the month has 29 days, then Date14 would return “29”; if the month has 30 days, then Date 14 would return “29” and Date15 would return “30”, etc.

This form may be filled out for a non-current month, so I can’t have it pick up the current month. For example, I may fill it out for the month of October on November 1st.

Thank you thank you thank you thank you!!!!

Lt. Gene Schrieber

Alabama Alcoholic Beverage Control Board

Enforcement District 1

P.O. Box 1755

Florence, Alabama 35631

(256) 764-2435

(256) 766-1793 Fax

www.abc.alabama.gov

"It's not about where you've been, it's where you are going that matters"

Avatar

Level 10

Hi Gene-O,

I'n not sure how your user is selecting the date so can't be exact but you should be able to replace the code.

var today = new Date();

util.printd("yyyy-mm-dd", new Date(today.getFullYear(), today.getMonth(), 1));

with something like;

var currentMonth = util.scand("yyyy-mm-dd", CurrentMonth.rawValue);

util.printd("yyyy-mm-dd", new Date(currentMonth.getFullYear(), currentMonth.getMonth(), 1));

This assumes they are using a DateTime field to select the current month, if they are using a drop down then it would need a bit more work.

If you can publish (and share) your form on Acrobat.com (or similar) I'll have a look.

Avatar

Level 10

Hi,

here's a FormCalc script which determines the number of days of the month that is currently selected in a date field.

It is doesn't matter which day of a month is selected.

If you want to display the last day, change the last code line into

Avatar

Former Community Member

Thank you for your reply!

However, this script is returning a value of “31” no matter which month I input in the date field.

Here is my script – my date field is named “PPSD1”:

var nDate = Date2Num(PPSD1.formattedValue, "DD/MM/YYYY")

var xDate = nDate

while (Num2Date(xDate+1, "M") eq Num2Date(nDate, "M")) do

xDate = xDate + 1

endwhile

$ = Num2Date(xDate, "D")

I have this in a calculate event of a NumericField.

Any ideas?

Lt. Gene Schrieber

Alabama Alcoholic Beverage Control Board

Enforcement District 1

P.O. Box 1755

Florence, Alabama 35631

(256) 764-2435

(256) 766-1793 Fax

www.abc.alabama.gov

"It's not about where you've been, it's where you are going that matters"

Avatar

Former Community Member

Paul,

How do I share the form on Acrobat.com for you to look at?

thanks,

Gene-O

Avatar

Correct answer by
Level 10

Maybe you're display pattern is different to the pattern in the script.

Here's a working example form:

https://acrobat.com/#d=rqvjWvn5WIBrWD*GLDRgfA