Calculating and Displaying Dates in Javascript | Community
Skip to main content
June 19, 2008

Calculating and Displaying Dates in Javascript

  • June 19, 2008
  • 20 replies
  • 26966 views
Hello,



I'm working on a flowchart form for my work, and am having issues getting a date to a) add correctly and b) display only certain portions of the date.



I have four fields:

CurrentDate - Displays the Current Date in MM/DD/YYYY format

DropDownList1 - Contains a list of projects end users can choose from

TextField1 - Displays text that tells the end user how many days the proejct will take

DateField1 - Should add the CurrentDate + the Number of days the project will take and return a new date in the same MM/DD/YYYY format



*Disclaimer* - I am not very experienced with scripting, and chose to go with JavaScript as I do have some previous past experience.



Here is what I have for my code right now:



if (DropDownList1.rawValue == 1){

TextField1.rawValue = "2 Days";

var AddDate = (Date.valueOf(CurrentDate) + (2*1000*60*60*24));

DateField1.formattedValue = Date(AddDate);



}



else if (DropDownList1.rawValue == 2){

TextField1.rawValue = "1 Day";

}



else if (DropDownList1.rawValue == 3){

TextField1.rawValue = "2 Days";

}



else if (DropDownList1.rawValue == 4){

TextField1.rawValue = "2 Days";

}



else if (DropDownList1.rawValue == 5){

TextField1.rawValue = "4 Days";

}



else if (DropDownList1.rawValue == 6){

TextField1.rawValue = "5 Days";

}



else if (DropDownList1.rawValue == 7){

TextField1.rawValue = "4 Day";

}



The end result is that the TextField1 displays the correct day, but the DateField1 displays a long string of the exact time and date of... well, the time - so no adding takes place. What I need is for the date to be displayed as MM/DD/YYYY. What am I doing wrong? I've only added the calculation to the first "if" statement, and will continue on to the rest once it is functioning.
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

20 replies

New Participant
December 10, 2014

I have a date field validated to display as MMYY. I want my date field to start at 0114. I need the end date to end at 1214. Is this possible? How would this be coded in java script or formcalc?

New Participant
February 6, 2009
The following code will work in a LiveCycle Designer button using the "JavaScript" language option:



var oNow = new Date(); // get date time object

console.show(); // oepn and clear the JavaScript console

console.clear();

console.println("oNow = " + oNow);

console.println("oNow.getFullYear() = " + oNow.getFullYear());

console.println("oNow.getMonth() = " + oNow.getMonth());

console.println("oNow.getDate() = " + oNow.getDate());

console.println("oNow.getDay() = " + oNow.getDay());

console.println("oNow.getHours() = " + oNow.getHours());

console.println("oNow.getMinutes() = " + oNow.getMinutes());

console.println("oNow.getSeconds() = " + oNow.getSeconds());

console.println("oNow.getTimezoneOffset() = " + oNow.getTimezoneOffset());



The following script uses FormCalc and can be used with a button for testing:



var oDate = Date() // get today's date

var Year = Num2Date(oDate, "YYYY") // get year four digits

var Month = Num2Date(oDate, "MM") // get month 2 digits with leading zero

var Date = Num2Date(oDate, "D") // get date without leading zero

xfa.host.messageBox( Concat("oDate: ", oDate, " Year: ", Year, " Month: ", Month, " Date: ", Date) )

var MyDate = "Feb 23, 2008" // a string data

var Year = Num2Date( Date2Num(MyDate, "MMM D, YYYY"), "YY" ) // 2 digit year

var Month = Num2Date( Date2Num(MyDate, "MMM D, YYYY"), "M" ) // month without leading zero

var Date = Num2Date( Date2Num(MyDate, "MMM D, YYYY"), "DD" ) // day with leading zero

xfa.host.messageBox( Concat("MyDate: ", MyDate, " Year: ", Year, " Month: ", Month, " Date: ", Date) )
New Participant
August 14, 2013

Is there an available sample form where this is working?

February 5, 2009
Hi ,

I am having a tough time trying to use the inbuilt Date functions here in Adobe Lifecycle designer in Javascript...

None of the functions like getFullYear(),getMonth(),getDate(),Date2Num and so on seem to be working.

Are this functions compatible only with FormCalc??

what could the issue be..

Please help..



Thanks,

Taran
January 22, 2009
i need to know how to make a number field calculate to boxes for example: 1st feild is quantity= 3, unit price per item is $5.00. i need my total box to multiply quantiy time unit price
December 23, 2008
Hi Geo Kaiser Thanks for the post but I am getting this error what does it means Microsoft JScript runtime error: 'util' is undefined

Please let me know



Thanks,

sandeep
December 9, 2008
I am using a form that is filled in by outside techs. There was a problem with them manipulating the date the form was written, so I entered a field for CurrentDate. This would force the date to appear when they wrote the form. Problem is that when the form is opened up days later at the office, the date that appears when we open it up is today. What will make the date stay for when the form was originally written without the techs having edit capability?
December 8, 2008
Thanks for your help. I ran across another interesting problem. I am using a form that is filled in by outside techs. There was a problem with them manipulating the date the form was written, so I entered a field for CurrentDate. This would force the date to appear when they wrote the form. Problem is that we the form is opened up days later, the date that appears when we open it up is today. What will make the date stay for when the form was written?
New Participant
December 3, 2008
You have to specify that you are using the "formattedValue" and not the default 'rawValue'. The date string being processed has to comply with the format string for the conversion.
December 3, 2008
Here is what I have to no avail.

form1.#subform[0].NumericField1=Date2Num("form1.#subform[0].DateTimeField2", "MMM D, YYYY") - Date2Num("form1.#subform[0].DateTimeField1", "MMM D, YYYY") Result in numeric field is always 0.
New Participant
December 2, 2008
For dates within the same month, one could extract the date with some string manipulation, but if the months are different this would make the code more complex then learning to to use the Date2Num() funcition.



This is not that hard.



1. Make sure you have the 2 dates

2. Know the format of the date fields

3. Use the Date2Num() function with the formatted values and format to get the number of days from the base Epoch date

4. Do the subtraction