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

June 19, 2008
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.
June 19, 2008
Date2Num and Num2Date are FormCalc functions. The code I'm using is in JavaScript
June 19, 2008
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)));
June 19, 2008
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?
June 21, 2008
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)) );
August 27, 2008
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
August 27, 2008
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")
October 3, 2008
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
October 5, 2008
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.
December 2, 2008
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......