Expand my Community achievements bar.

Adobe LiveCycle Designer 8 - Add days to Current Date in another text field

Avatar

Former Community Member
Hi-

I am working on an expense report. I have six fields, CurrentDate, and countDate1 through countDate5. The CurrentDate is a Time/Date field which the user can select whatever date is needed with the drop down calendar. The other five countDate fields are "text" fields which will represent Monday through Friday. I would like to add zero days to whatever the user selects as the CurrentDate and make that appear in countDate1 which represents Monday(the CurrentDate the user selects will always be a Monday), add one day to whatever the user selects as the CurrentDate and make that appear in countDate2 which represents Tuesday...and so on. I realize this is probably basic for someone familiar with FormCalc but I'm very new at this.



This got me very close but I want the user to select the date and not have the CurrentDate already filled in.



CurrentDate - DateTime field, FormCalc calculation script:

num2date(Date())



Date1 - Text field, FormCalc calculation script:

Num2Date( Date2Num(CurrentDate.formattedValue))



Date2 - Text field, FormCalc calculation script:

Num2Date( Date2Num(CurrentDate.formattedValue) + 1 )



Thanks!

Brian
14 Replies

Avatar

Level 7
You may need to provide the optional "format" value for the various date fields to get this to work.



CurrentDate - DateTime field, FormCalc calculation script:

num2date(Date(), "MMM DD, YYYY")



Date1 - Text field, FormCalc calculation script:

Num2Date( Date2Num(CurrentDate.formattedValue, "MMM DD, YYYY"), "MMM DD, YYYY")



Date2 - Text field, FormCalc calculation script:

Num2Date( Date2Num(CurrentDate.formattedValue, "MMM DD, YYYY") + 1, "MMM DD, YYYY")



Your date fields should also be a text field as the scrips are establishing the formatting of the data.



The date and time functions are dealing with a formatted string that is either being converted to a number or a number being converted to a formatted field and the script need to know the exact format for converting this data.

Avatar

Former Community Member
Geo,

Thanks for your help! With the above script you gave me it is working. However, are you referring to the "CurrentDate" Date/Time field being a text field as opposed to the Date/Time field which allows the user to select a date from the calendar because of formatting issues. As of now I have it as a Date/Time field but it automatically populates with the current date. You must click on the calendar to edit it. When you do click on the calendar and select a new date it asks "Are you sure you want to edit this field".



Also, when you do edit the calendar, if you go backwards more than one week it incorrectly calculates the other Date1 through Date5 text fields by starting at Jan. 01 1900. You can go forward as much as you like and there is no problem with that.

Avatar

Former Community Member
I am trying to do something close to this and I have tried un-succesfully to use the script you wrote.

My problem is I have a Date Field (Picker) that picks a date in the future. I need to include on my form several fields that automatically displays dates that are in the future at various intervals like 6 mo. - 5 mo. - 2 mo. - 14 days - 7 days out from the original date selected.



I tried to use both date and text field types but I have yet to get it to work.



Any Help ( I'm a New-B ) would be appreciated.

Avatar

Level 7
Here is an exmaple of adding days the script is used in the "exit" event for the date select field that has display format of "MM/DD/YYYY". Adding days requires add x number of days to the days since the epoch date for the current date, adding months or years one needs to manipulate the string parts of the date.



----- form1.#subform[0].InputDateField::exit: - (FormCalc, client) ---------------------------------



// fomatted string for selected date

var sFmtDateValue = $.formattedValue

var sMsg = Concat("Entered date formatted: ", sFmtDateValue) // build message string

sMsg = Concat(sMsg, "\u000a" ) // add new line to message

// convert date string to days since epoch date - format is important

var fDaysPast = Date2Num(sFmtDateValue, "MM/DD/YYYY")

// add 7 days to days past epoch date

var f7DaysPlus = fDaysPast + 7 // add 7 days

var s7DaysPlus = Num2Date(f7DaysPlus, "MMM DD, YYYY") // format string for 7 days plus

sMsg = Concat(sMsg, "\u000a", "Plus 7 Days: ", s7DaysPlus) // build message string

// add 14 days to days past epoch date

var f14DaysPlus = fDaysPast + 14 // add 7 days

var s14DaysPlus = Num2Date(f14DaysPlus, "MMMM DD, YYYY") // format string for 7 days plus

sMsg = Concat(sMsg, "\u000a", "Plus 14 Days: ", s14DaysPlus) // build message string

// display results



// work on months

// get parts of date past epoch date

var sFullYear = Num2Date(fDaysPast, "YYYY") // get 4 digit year form days past epoch date

var sMonth = Num2Date(fDaysPast, "MM") // get month form days past epoch date as number

var sDate = Num2Date(fDaysPast, "DD") // get date form days past epoch date as a number



var s2Month = Sum(sMonth, 2) // add 2 months

var s2FullYear = sFullYear

// if more than 12 months in new date adjust year on number of months

if (s2Month > "12") then

s2FullYear = Sum(s2FullYear, + 1) // increment year

s2Month = Sum(s2Month, - 12) // adjsut months

endif

var s2MonthsAdded = Concat(s2Month, "/", sDate, "/", s2FullYear) // date string

sMsg = Concat(sMsg, "\u000a", "Added 2 months: ", s2MonthsAdded) // display stringxfa.host.messageBox(sMsg, "Sample Adding Days" ,3, 0);



var s5Month = Sum(sMonth, 5) // add 5 months

var s5FullYear = sFullYear

// if more than 12 months in new date adjust year on number of months

if (s5Month > "12") then

s5FullYear = Sum(s5FullYear, + 1) // increment year

s5Month = Sum(s5Month, - 12) // adjsut months

endif

var s5MonthsAdded = Concat(s5Month, "/", sDate, "/", s5FullYear) //build Date string

sMsg = Concat(sMsg, "\u000a", "Added 5 months: ", s5MonthsAdded) // display stringxfa.host.messageBox(sMsg, "Sample Adding Days" ,3, 0);



// display results

xfa.host.messageBox(sMsg, "Sample Adding Days and Months" ,3, 0);

Avatar

Former Community Member
Geo,



Is it possible to email a sample 3 line 3 field pdf form to you or anyone that can help.



Unfortunately for me this is the first time for me using LiveCycle Designer. I think I have a handle on most of it but writing scripts is currently beyond my capability. I have created a simple three line pdf file that contains 3 fields. The first field is a date picker that the user chooses a FUTURE DATE. The other two fields I want to be automatically filled in depending on the future date selected in the first field. The other two fields should calculate dates BEFORE the date selected.



What I am trying to set up is when a FUTURE DATE (which can change) is selected using the date picker the other FIELDS displays time periods BEFORE the FUTURE DATE as (earliest DATE to START) and ( the last possible DATE to FINISH)



This is part of a TO DO LIST with a SET TIME for completion of the project.



Example:



BY JULY 10, 2009 (DUE DATE) Items A thru D must be completed between February 10, 2009 and March 10, 2010 (6 MONTHS to 5 MONTHS prior to DUE DATE.



Items E thru J must be completed between March 10, 2009 and April 10, 2010 (5 MONTHS to 4 MONTHS prior to DUE DATE.



Items K thru N must be completed between April 10, 2009 and May 10, 2010 (4 MONTHS to 3 MONTHS prior to DUE DATE.



And so on until finally a date no later than one week prior to the DUE DATE.



Thanks for your time and consideration

Avatar

Former Community Member
I have a similar problem. I have a DateTimeField3 which has a date to be entered. In DateTimeField4 I want it to calculate the date plus 280 days. I am at a loss on how to do this. I have tried to put the script $ = num2date(DateTimeField+280) in the DateTimeField4 but get no results.



Can you help?



Thanks

Avatar

Former Community Member
I'm in the same boat as Allan. I'm trying to add 14 to the current date. Nothing seems to work. Any ideas? Thanks.

Avatar

Level 7
Because formatted date strings are language dependent, you should specify the optional formatted date string pattern.



With a field named "MyDate" and a formatted value of "MMM DD, YYYY", the script to add 14 days in FormCalc is:



if(HasValue(MyDate)) then

Num2Date( (Date2Num(MyDate.formattedValue, "MMM DD, YYYY") + 14), "MMM DD, YYYY")

else

null

endif



You may also want to add a validation format to ensure that the inputted date is acceptable.

Avatar

Former Community Member
HI Geo,

doing someting very similar as above- have changed the object names but i'm still getting an error



here is the snippett:

if(HasValue(sundaystartdate.formattedValue,"mmm d, yyyy"))then

MondayDate((Date2Num(sundaystartdate.formattedValue,"mmm d, yyyy")+1),"mmm d, yyyy")

else

null

endif



here is the error i'm getting: function 'MondayDate' on line....is unknown'



is this a simple syntax error or did i miss a bigger piece somewhere?



thanks- from a complete noob....

eric

Avatar

Former Community Member
OK- i see part of the error of my ways: you change the entered date to a number, add 1 then change it back to a formatted date:



with that 'new date' i want to assign it to a text box, so the snippet now looks like this:

if(HasValue(sundaystartdate.formattedValue,"MMM D, YYYY"))

then form1.weeklyblock.MondayDate((Num2Date(Date2Num(sundaystartdate.formattedValue,"MMM D, YYYY")+1 ),"MMM D, YYYY"))



else

null

endif



but it's throwing a syntax error just after i add 1 day- anyone know what i'm doing wrong?



TIA

e

Avatar

Former Community Member
HI All please ignore the above- i've figured out what the problems were..and it ws the loose nut at the keyboard.....



new problem- associated to above. user clicks on a date and it fills in the rest of the week. This is only set to work if the user chooses a Sunday.



my ?: how do i validate that the user choose a 'sunday'?



FWIW- i tried a JS calendar where i could limit dates shown, but couldn't get it to show just sundays...so i thought i'd try to just validate that the user chooses the correct date and throw an error if not.



any ideas?



TIA

eric

Avatar

Former Community Member
HI All,

still wondering if there is a way to limit users to choosing just 'sundays' from the pop up calendar?



I have everything else working.



thanks

eric

Avatar

Level 2

I have the same problem, where you ever able to find a solution to this problem?

Avatar

Level 7

You will need to add instructions for the user and write a validation script that checks if the  'day of the week'  selected is a correct date or not.

You can extract the 'day of the week' from the number value for the date by using the format string for the day of the week in the "Num2Date()' funciton format option.

// returns the day of the week 1 - 7 day of the week, Sunday = 1

Num2Date(fDateValue, "E")

// returns the abreviated day of week - 3 character value

Num2Date(fDateValue, "EEE")

// returns full day of the week

Num2Date(fDateValue, "EEE")