Expand my Community achievements bar.

Calculating and Displaying Dates in Javascript

Avatar

Former Community Member
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.
21 Replies

Avatar

Level 7
Look at using the "Date2Num()" and the "Num2Date()" functions to convert the string date value to the number of days from the Epoch date, do the math, and then convert back to date string. More information is in LiveCycle Designer's "Scripting Reference" under the "Help" menu option.

Avatar

Former Community Member
Date2Num and Num2Date are FormCalc functions. The code I'm using is in JavaScript

Avatar

Level 7
In LiveCycle Designer one can call the FormCalc functions in JavaScript just like you are accessing the fields and their properties similar to FormCalc. This maybe easier than some fo the JavaScript procedures.



JavaScript using the year, month, and date not the milliseconds:



// get current date time object

var oCurrentDate = new Date();

// get the year, month and date from the date time object

var CurrYear = oCurrentDate.getFullYear(); // get 4 digit year number

var CurrMonth = oCurrentDate.getMonth(); // get current month number

var CurrDay = oCurrentDate.getDate(); // get current date number

var AddDays = 0; // assume no adjustment



switch(DropDownList1.rawValue) {

case 1:

case 3:

case 4:

AddDays = 2;

break;



case 2:

AddDays = 1;

break;



case 5:

case 7:

AddDays = 4;

break;



case 6:

AddDays = 5;

break;



default:

TextField1.rawValue = "0 Days";

AddDays = 0; // assume no adjustment

break;

} // end switch DropDownList1



// build the notification and formatted date string

if(AddDays == 0) TextField1.rawValue = '';

if(AddDays == 1) TextField1.rawValue = AddDays + " Day";

if(AddDays > 1) TextField1.rawValue = AddDays + " Days";

DateField1.formattedValue = util.printd("mm/dd/yyyy", Date(CurrYear, (CurrMonth - 1), (CurrDay + AddDays)));

Avatar

Former Community Member
Thanks for the help Geo, I really appreciate it. I pasted the code into my form, and am getting some strange results. TextField1 is staying blank between all options and the DateField1 is returning out of the ballpark numbers like 2089/-6924/13532 and 482/-6924/-4140. I've reviewed the code, it looks solid. Any idea why this is happening?

Avatar

Level 7
Use the following JavaScript for the calculation script for DateField1:



var AddDays = 0;

// select action on value of DropList1 to set days to add

switch(DropDownList1.formattedValue) {

case "1":

AddDays = 1;

break;

case "2":

case "4":

AddDays = 2;

break;

case "3":

AddDays = 3;

break;

case "5":

case "7":

AddDays = 4;

break;

case "6":

AddDays = 5;

break;

default:

AddDays = 0;

break;

} // end shitch



var oCurrDate = new Date(); // get the curred date object

var fYear = oCurrDate.getFullYear(); // get full year

var fMonth = oCurrDate.getMonth(); // get current month

var fDate = oCurrDate.getDate(); // get current date

// fix text display for days to add

if (AddDays == 1) $.TextField2.formattedValue = AddDays + " day";

else $.TextField2.formattedValue = AddDays + " days";



// some debugging and tracing information

console.show(); console.clear();

console.println(DropDownList1.rawValue + " a:" + AddDays + " y:" + fYear + " m:" + fMonth + " d:" + fDate);

console.println( new Date(fYear, fMonth, (fDate + AddDays) ) );

console.println(util.printd("mm/dd/yyyy", new Date(fYear, fMonth, (fDate + AddDays)) ) );

console.println(AddDays + " days");



// format comuted date for display

$.formattedValue = util.printd("mm/dd/yyyy", new Date(fYear, fMonth, (fDate + AddDays)) );

Avatar

Former Community Member
Hi



In the Time sheet form I am designing, I would like the day to be automatically displayed, based on the date entered.



For instance, if the date entered is 27 August 2008, I want the day Wednesday to appear alongside.



Is there any function or coding that can be used for that purpose.



Ashvin

Avatar

Level 7
You can use the format string in the "Num2Date()" function or the 'util.printd()" method to get the day of the week.



For FormCalc:



// get the number of days from the Epoch date

var nFrEpoch = Date2Num("27 August 2008", "DD MMMM YYYY")



// long day of week

Num2Date( nFrEpoch, "EEEE")



// short day of week

Num2Date( nFrEpoch, "EEE")



// day number of week zero based

Num2Date( nFrEpoch, "DD MMMM YYYY"), "E")

Avatar

Level 1
I am trying to do something similar and am not Java Script savvy in the least. I have a document I've created that if a student would select the days Monday - Thursday I would like the time field to only allow 7:30 am - 7:30 pm to be entered on those days and only the hours of 8:30 - 5:30 for Fridays. Is there a way to write this and add it in as script if I've just been using the design view for all my form design so for in Live Cycle?



Thanks- Kara

Avatar

Level 7
You should do a new post and not hihack an exisiting post.



You might find this calculation easier in FormCalc. There is a Time2Num() function that will convert a time stirng, that is a alphanumeric string and not a number string, to the number of milliseconds since LiveCycle Designer's Epoch date. With this value you can do many computations and then you will need to reformat your numeric answer back to a text string with the numbers and seperators as needed.

Avatar

Former Community Member
I need help! I am new to Adobe and just learning formcalc and some Javascript from these messages. How do I calculate the number of days from two date fields. See below:



RepairTable.#subform[0].DateTimeField1[1] (let's say it is Dec 10, 2008

RepairTable.#subform[0].DateTimeField1[0] (let's say this is Dec 5, 2008

Can I have a numeric field that will subtract the 5th from the 10th and equal 5?

Ron......

Avatar

Level 7
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

Avatar

Former Community Member
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.

Avatar

Level 7
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.

Avatar

Former Community Member
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?

Avatar

Former Community Member
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?

Avatar

Former Community Member
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

Avatar

Former Community Member
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

Avatar

Former Community Member
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

Avatar

Level 7
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) )

Avatar

Level 2

Is there an available sample form where this is working?