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

Date calculation using YYYYMMDD

heartles187
Registered: Sep 7 2007
Posts: 6

I'm having problems calculating the difference between two dates from two fields that use a date format of "YYYYMMDD". As an example they enter in the first field 20080106 and in the second field they enter 20070804. A third field will show how many months and days there are between those two dates. I got this calculation working using mm/dd/yyyy but for some reason it will not calculate using yyyymmdd. How can I get this to work using YYYYMMDD in both fields?

My Product Information:
Acrobat Pro 6.0.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Read this:

[url=http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/date_time_part1/]http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/date_time_part1/[/url]

It sounds like your problem is not so much with date calculations but with Parsing the date. When asking a question here please be specific. Tell us your exactly what it is you are doing and exactly what the problem is.

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

heartles187
Registered: Sep 7 2007
Posts: 6
Thom,

I already read those pages the util.scand("yyyymmdd", does not work using yyyymmdd. I created the following code to allow it to calculate the difference in days between the "ECC" field and the "REENLIST" feild. It calculation is placed in a third feild and it calculates the days perfectly. Please check out the code and let me know if there is a simpler way to do this.

var ECCDate = this.getField("ECC").valueAsString;
var EnlistDate = this.getField("REENLIST").valueAsString;

var ECCYear = ECCDate.substring(0,4);
var ECCMonth = ECCDate.substring(4,6);
var ECCDay = ECCDate.substring(6,8);

var EnlistYear = EnlistDate.substring(0,4);
var EnlistMonth = EnlistDate.substring(4,6);
var EnlistDay = EnlistDate.substring(6,8);

var newECCDate = (ECCMonth + "/" + ECCDay + "/" + ECCYear);

var newEnlistDate = (EnlistMonth + "/" + EnlistDay + "/" + EnlistYear);

var strStart = newEnlistDate;
var strEnd = newECCDate;

var dateStart = util.scand("mm/dd/yyyy",strStart);
var dateEnd = util.scand("mm/dd/yyyy",strEnd);
var diff = dateEnd.getTime() - dateStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = (diff/oneDay - 2);
event.value = days;

As you can see I had to change the input from YYYYMMDD to mm/dd/yyyy in order to have java calculate the difference between the days. Now I'm having problems finishing the code. I would like to use the "days" value to finish my required calculation, I need to show how many Years, Months, and Days are left between those two dates. Example: ECC = "20080106" and REENLIST = "20070804", the OS field will display "0Y 5M 3D". I can not seem to get this to work because it keeps rounding the days.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, you can make this a little simpler. This code is performing what Scand does, You can go right from your parsed data into a date object, like this:

var dateSart = new Date(EnlistYear, EnlistMonth, EnlistDay,0,0,0,0);

The only other thing you might do is use regular expressions to do the parsing, But otherwise your code is great!!

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

heartles187
Registered: Sep 7 2007
Posts: 6
Thank you, that shortened the code a little. Add this to the bottom of the code above and if you could let me know what is wrong with this. I can not seem to get it to display the correct information. This was what I was trying to ask in the second part of my question above. I want a feild to display how many years, how many months, and how many days are between the two dates. This is just trying to find out how many years and months are between the two days, then I can take those two numbers and subtract them to get how many days are left. For some reason it is rounding the numbers on me, if I have 1.5 years it will display 2 years.

if(days < 365)
{
var TotalYears = 0;
}
else
var TotalYears = (days/365);
}
if(days >= 365)
{
var yeartodays = (TotalYears * 365);
var daysleft = (days - yeartodays);
}
else
{
var daysleft = days;
}
if (daysleft < 30)
{
var TotalMonths = 0;
}
else
{
var TotalMonths = (daysleft/30);
}
event.value = TotalMonths;
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There's nothing in your calculations that would suggest that "TotalYears" is rounded up. When you say "if I have 1.5 years it will display 2 years". Do you mean that it's only rounded up in the form field? "TotalYears" is not assigned to a form field in this code. How is this done? Have you checked the formatting on the "TotalYears" field. Have you placed "console.println()" statments in this code to check the intermediate values?

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

kummvel
Registered: Apr 8 2008
Posts: 1
Hi experts,
can anyone tell me i am using interactive form,i want to calculate between two dates like STARTDATE and ENDDATE.When i select the event CALCULATE,the getField is not coming instead of getAttribute and getLayout is coming.I am the new from Interactive form.
In my Form i used StartDate and EndDate also NumberOfDays.When i am entering the date from EndDate the total can come to DAYS field.

Please give me your Feasible Answer,

Advance thanks and Regards,