Expand my Community achievements bar.

difference between two dates in javascript

Avatar

Former Community Member

Hi can someone showme the code for how to calculate the difference between two dates.

i have two fields.

date in and date out

i need to figure out how to find teh difference between the two dates in order to fire off another code.

i used formcalc previously but i can't get the java to read the results of the formcalc cell (hidden).

i would prefer to calculate the days within the script. (i have used the partNoScript from the Purchase order example)

can someone help ?

the fields are in a table in the same row.

i'm tring to design a form where you select a car to hire. this will fill in a field called cost.

however depending on the number of days the car is needed there will be different pricing. so i need to calculate difference between two dates.

i've read some tutorials out there but they are for acrobat. and the code doesn't work in LC.

4 Replies

Avatar

Level 4

And again I can't open the Attachment.

Can you tell me how you did manage to populate the hidden field? *likes to have new scripts in her collection*

Or you could send me a mail: diefeuerwolke@web.de

Avatar

Level 10

Hi,

Dates are best handled in FormCalc.

Something like the following in the calculate event of the "number of days" field in your table:

if (dateOut == null or dateIn == null ) then
    $ = null
elseif (dateOut > dateIn) then
    xfa.host.messageBox("Please check the In and Out dates, as there appears to be an error", "Hire Dates")
    $ = null
else
    $ = Date2Num(dateIn, "YYYY-MM-DD", "en_IE") - Date2Num(dateOut, "YYYY-MM-DD", "en_IE")
endif

Date2Num turns the dates into numbers. JavaScript should be able to read / handle the result without difficulty.

In the price field, you would have an if statement going through the price bands for increasing number of days.

The help file in LC provides FormCalc date functions, date patterns and locale identifiers.

Hope that helps,

Niall

Avatar

Level 4

I wanna see your solution though.

Probably I can fix your problem then...

(If you already have the number of days I think it is a event problem... though it could probably be some kind of a formatting problem too.)

Could it be, one of the dates is the "today's date"?

Avatar

Level 1

Here is some working JavaScript code from one of my forms that you might be able to adapt to your problem:

form1.#subform[0].TextFieldAge::calculate - (JavaScript, client)
// Given a Date/Time Field ("DateTimeFieldDOB") and a Text Field ("TextFieldAge")
// calculate TextFieldAge in years based on DateTimeFieldDOB and the current date
var strDateBirth = DateTimeFieldDOB.rawValue;  // Returns String in format "YYYY-MM-DD"
if ((strDateBirth != null) && (strDateBirth != ""))
{
   // Change date format to "YYYY/MM/DD"
   var strDateBirthSlash = strDateBirth.replace(/-/g, "/");

   // Convert string to Date object
   var dateBirth = new Date(strDateBirthSlash);
   var dateToday = new Date();  // Get the current date

   // Get difference between dates in milliseconds
   var nAgeMilliseconds = dateToday.getTime() - dateBirth.getTime();

   // Define number of milliseconds in one year
   var nMilliSecondsPerYear = 365 * 24 * 60 * 60 * 1000;

   // Get age in years (does not account for leap years, but is close enough)
   var nAgeYears = Math.floor(nAgeMilliseconds / nMilliSecondsPerYear);

   TextFieldAge.rawValue = nAgeYears;
}