These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Populate Date/Days of Week between two Dates

br27ke
Registered: Nov 12 2009
Posts: 21

Hey -
I am working on a semi-monthly timesheet form where the user will enter the period ending date. I'd like for the day of the month fields (1st, 2nd, etc) and the day of the week fields (M, T, W, etc) to auto-populate if possible. I am open to having the user enter both the period start date and period end date if necessary. I'm using Livecycle and most of the posts I've read regarding this are counting the number of days in between. Any suggestions you have would be great - Thanks!

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Have you read LiveCycle Designer's Scripting Reference?

LiveCycle Designer has a number of functions for manipulating dates. LiveCycle Designer use the number of days from a fixed Epoch date for manipulation and creating various date strings.

The 'Date()' function returns the number of days from the Epoch Date. You can than perform addition and subtraction with this value to move forward or backward a given number of days. With the value for number of days from the Epoch Date, one can get or create various strings that display information about date for that number by using the 'Num2Date()' function and passing the number of days from the Epoch Date and a specific format string.

The following code can be used with a button to display information about today and information about the date in 3 days.
// get today's datevar Now = Date()// format for 4 digit yearvar sFullYear = Num2Date(Now, "YYYY")// format for 3 character monthvar sMonth = Num2Date(Now, "MMM")// format for 2 digit datevar sDate = Num2Date(Now, "DD")// format for day of week as 3 character fieldvar sDay = Num2Date(Now, "EEE")// display information about today:var sMsg = Concat("Today is ", sMonth, " ", sDate, ", ", sFullYear)sMsg = Concat(sMsg, "\u000d", "The day of the week is ", sDay)sMsg = Concat(sMsg, "\u000d", "It has been ", Now, " days since the Epoch Date")xfa.host.messageBox(sMsg, "Date Information", 3, 0)// in 3 daysNow = Now + 3// format for date mm/dd/yysDate = Num2Date(Now, "MM/DD/YY")// format for day of weeksDay = Num2Date(Now, "EEEE")sMsg = Concat("In 3 days it will be ", sDate)sMsg = Concat(sMsg, "\u000d", "The day of the week will be ", sDay)sMsg = Concat(sMsg, "\u000d", "It will be ", Now, " days since the Epoch Date")xfa.host.messageBox(sMsg, "Date Information", 3, 0)

George Kaiser

br27ke
Registered: Nov 12 2009
Posts: 21
Thanks for your reply, I'll admit I'm fairly new to Livecycle and Javascript and am learning as I go. I have read through the reference but have a hard time making it into real examples. I was able to get the date working, but it was using FormCalc and I'd rather have it in Java so that I can use an IF statement to control how long each PP is. Our PP are not a fixed number of days but rather a fixed start/stop.

I'll try to do some more digging with what you gave me - Thanks!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Then you can use the JavaScirpt date object and it's various methods. You could also use the Acrobat JS 'util.printd()' method to format the date information dispaly.
// get today's date objectvar oNow = new Date();var sMsg = "Today is " + util.printd("mmm d, yyyy", oNow);sMsg += "\n" + "The day of the week is " + util.printd("ddd", oNow);app.alert(sMsg, 3, 0)// add 3 days and make new date objectoNow = new Date(oNow.getFullYear(), oNow.getMonth(), (oNow.getDate() + 3) )// dispaly new date informationsMsg = "In 3 days it will be " + util.printd("mm/dd/yyyy", oNow);sMsg += "\n" + "The day of the week will be " + util.printd("dddd", oNow);app.alert(sMsg, 3, 0)

George Kaiser

br27ke
Registered: Nov 12 2009
Posts: 21
Again, I'm probably overlooking something relatively simple, but both of your examples uses a fixed number of days to return the new day. Our PP runs from the 2nd-16th and 17th-1st, meaning they are not a fixed number of days. I can find the new day and day of week for each of my day columns, but how can I control how many columns populate these based on the end date?

I'm sure I need to first do something to calculate the number of days in the pay period and make that a variable, but then how can I make the variable decide how far out to take the script...

I feel like this should be easy, but the more I look at examples, the more confused I become.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You can split strings and build new values for creating new date objects.
// define some time constantsvar fSec = 1000; // number of milliseconds in a secondvar fMin = 60 * fSec; // number of milliseconds in a minutevar fHour = 60 * fMin; // number of milliseconds in an hourvar fDay = 24 * fHour; // number of milliseconds in a day // get month and year for pay periodsvar cResponse = app.response("Enter Month and Year for payroll period (MM/YYYY):");var aResponse = cResponse.split("/"); // split into month and year // first pay periodvar oStart1 = new Date(aResponse[1], (aResponse[0] - 1), "02"); // date object start1var fStart1 = Math.floor(oStart1.getTime() / fDay); // number of days for object start1var sStart1 = util.printd("mm/dd/yyyy", oStart1); // date string start1var oEnd1 = new Date(aResponse[1], (aResponse[0] - 1), "16"); // date object end1 datevar fEnd1 = Math.floor(oEnd1.getTime() / fDay); // number of days for object start1var sEnd1 = util.printd("mm/dd/yyyy", oEnd1); // date string end1// compute days for first pay periodvar fDays1 = fEnd1 - fStart1 + 1;app.alert("The number of days in pay period " + sStart1 + " to " + sEnd1 + " is " + fDays1 + " days.", 3, 0); // second pay period// first pay periodvar oStart2 = new Date(aResponse[1], (aResponse[0] - 1), "17"); // date object start2var fStart2 = Math.floor(oStart2.getTime() / fDay); // number of days for object start2var sStart2 = util.printd("mm/dd/yyyy", oStart2); // date string start1var oEnd2 = new Date(aResponse[1], aResponse[0], "01"); // date object end1 datevar fEnd2 = Math.floor(oEnd2.getTime() / fDay); // number of days for object start1var sEnd2 = util.printd("mm/dd/yyyy", oEnd2); // date string end1// compute days for first pay periodvar fDays2 = fEnd2 - fStart2 + 1;app.alert("The number of days in pay period " + sStart2 + " to " + sEnd2 + " is " + fDays2 + " days.", 3, 0);

George Kaiser

br27ke
Registered: Nov 12 2009
Posts: 21
Thanks for your help, I'm obviously way over my head because I can't seem to get anything to work now despite reading through the scripting reference, your examples, and T. Parkers date/time tutorials. May just go back to using our Excel one. Thanks again.