These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Convert date and time into minutes

degracia1
Registered: May 9 2011
Posts: 6

hi there
I need to find the difference in minutes between 2 datetime fields that I have on a single form. I need to store the result in a field called mindiff.
 
startDateAndTime : the user enters the value in this field in the following format dd/mm/yyyy hh:mm
endDateAndTime : the user enters the value in this field in the following format dd/mm/yyyy hh:mm
 
How can I do this in Livecycle , either using formcalc or javascript ?
 
at the end i need to convert the date and the time into minutes and subtract date from time. is that possible?
 
i have all these fields in 1 tables in a subform and i'm using FormCalc.
thanks for your assistance. .

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Converting a date in a known format into minutes is fairly simple using JavaScript.

See this article:
http://acrobatusers.com/tutorials/2006/date_time_part2/

The sequence goes like this:
1. Use "util.scand()" to convert the formatted date/time text into a Date Object. This is the key part.

var oStartDate = util.scand("dd/mm/yyyy/ hh:MM", startDateAndTime.rawValue);


2. convert the Date object into milliseconds
3. convert milliseconds into minutes
4. compute difference

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

degracia1
Registered: May 9 2011
Posts: 6
hi there,
thank you for your reply:
this is what i tried to do from the guidelines you sent me but its still not working, what am i doing wrong?

var oStartDate = serviceStartDateTime.rawValue;
var oEndDate = serviceEndDateTime.rawValue;
var diff = 0;
var oneDay = 0;
var days = 0;

oStartDate = this.getAttribute("serviceStarDateTime").value;
oEndDate = this.getAttribute("serviceEndDateTime").value;
if(oEndDate.length||oStartDate.length)
{
oStartDate = util.scand("dd/mm/yyyy/ hh:mm", oStartDate);
oEndDate = util.scand("dd/mm/yyyy/ hh:mm" , oEndDate);
// One Day = (24 hours) x (60 minutes/hour) x
// (60 seconds/minute) x (1000 milliseconds/second)
oneDay = 24 * 60 * 60 * 1000;
diff = oEndDate.getTime()- oStartDate.getTime();
days = Math.floor(diff/oneDay);
}
event.value = days;
end;
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The script looks sort of ok. There are a couple issues. You've re-used oStartDate and oEndDate inappropriately and there is no "end" statement.

// These first two values are strings
var strStartDate = serviceStartDateTime.rawValue;
var strEndDate = serviceEndDateTime.rawValue;
var diff = 0;
var oneDay = 0;
var days = 0;

if(strEndDate.length||strStartDate.length)
{
var oStartDate = util.scand("dd/mm/yyyy/ hh:mm", strStartDate);
var oEndDate = util.scand("dd/mm/yyyy/ hh:mm" , strEndDate);
// One Day = (24 hours) x (60 minutes/hour) x
// (60 seconds/minute) x (1000 milliseconds/second)
oneDay = 24 * 60 * 60 * 1000;
diff = oEndDate.getTime()- oStartDate.getTime();
days = Math.floor(diff/oneDay);
}
event.value = days;

This is the correct code, but it doesn't mean the script will work. Everything hinges on the field values being in the correct format. After you make the changes and test the form, look in the Acrobat Console window to see if any errors are reported.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

degracia1
Registered: May 9 2011
Posts: 6
hi again
i fixed my mistakees but i still dont get results, so i'm not sure what is it that i'm doing wrong.
could you please give some other suggestions that i can try out. i appreciate your help.
thanks.


thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Did you look in the Console window to see if an error is reported?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

degracia1
Registered: May 9 2011
Posts: 6
hi,
can i copy my code to the window console to see if it works....? i've never done this before so i dont know how to go about it. i've seen the video of the window console but now in my case as i have my code in livecycle designer i'm not sure how to do this. please assist.

thanks so much.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There is one other error in the code that I missed. Change the last line to just:

days;

remove the "event.value =" part.

This will not fix the script, it just makes it more correct.

With some changes the code can be run from the console window. The console window runs at the top of the scripting context so fields are referenced with the fully qualified SOM path. If you don't have a good handle on how this works I would suggest that instead you try the debugging technique discussed in the video, put "console.println()" statements in your code to print out intermediate values in the script.

Watch this video: http://acrobatusers.com/events/2008/04-0

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

degracia1
Registered: May 9 2011
Posts: 6
hello there

this is what i get from the javascript console from the javascript i sent you (see below), the challenge is i dont know what they mean since i'm new in javascript.

event.target.hostContainer is undefined
110:1Exception in line 110 of function RegisterMessageHandler, script 1
Exception in line 7 of function top_level, script 0

TypeError: event.target.hostContainer is undefined
110:1180000001
2 // no of days
Exception in line 22 of function top_level, script 0

GeneralError: Operation failed.
XFAObject.value:22:XFA:singleCall[0]:#subform[0]:callItems[0]:Table1[0]:Row1[2]:serviceMins[0]:exit
Invalid property set operation; value doesn't have a default property //please explain if you knwo what this means

thanks a lot.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Wow! Why does the form contain code to setup the HostContainer messaging? Get rid of this code or properly qualify it.

The last error looks like it might be significant. Did you place the script we've been discussing in the Exit Event? or is this some other piece of code?

Where did you get this form? Did you create it or are you modifying an existing form?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script