Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using getDate in input field, and displaying daysLeftUntil in another field...

New Here ,
Jan 30, 2013 Jan 30, 2013

I am trying to use JS in an adobe form I created. I have a date field in which the user enters the current date. Then, in another field, the days left until the end of the year are displayed. I can do this great in HTML, but behavior in Acrobat isn't quite the same.

Here's what I have so far, but applying it proves difficult.

function DayDiff(StartDate)

{

    var TYear=StartDate.getFullYear();

        var TDay=new Date("January, 01, 2014");

        TDay.getFullYear(TYear);

        var DayCount=(TDay-StartDate)/(1000*60*60*24);

        DayCount=Math.round(DayCount);

    return(DayCount);

}

80.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 31, 2013 Jan 31, 2013

Yes, probably... Change this:

var diffDays = diffMs / oneDay;

To:

var diffDays = Math.floor(diffMs / oneDay);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

First of all, what's the point of this line:

TDay.getFullYear(TYear);

? It doesn't change anything...

Furthermore, you should use the getTime() method on your Date objects to get the difference between them in ms, after which you can divide it to get the results in days (that you already do).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

You're right. It didn't anything, so I changed it. However, I'm still unable to have a user enter the date in one input box and have it output the number of days between that input date and the end of the year... How do I make that display happen. It's just not working for me. I have the display field set as "hidden but printable"...does that make any difference?     

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

You're close, but not quite there... What are passing to this function, exactly? Where are you converting the string the user inputs to a Date object?

If you want to set year of TDAY to the year following that the user entered, use this: TDay.setFullYear(TYear+1);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

You're close, but not quite there... What are passing to this function, exactly? Where are you converting the string the user inputs to a Date object?

I guess that's where I'm having trouble. In HTML, the variable for the input text box is the converted string... but in Adobe, I'm not sure how to pass something to this function.

I'm lost! Can you provide more detailed feedback?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

In Acrobat (also in HTML pages, I believe) you need to explicitly convert a

string into a Date object in order to be able to work with it as such.

There are two ways of doing it: The Date constructor (like you used to

create the other Date object) and an Acrobat-specific method called

util.scand(). The latter is somewhat better than the constructor because

you can specify a date pattern, for example:

util.scand("mm-dd-yyyy", "20-12-2013")

will return a Date object for Dec. 20th, 2013.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

Okay... so I research the util.scand() and re-wrote the whole thing using that function.

var strStart = this.getField("StartDate").value;

    if(strStart.length) {

         var dateStart = util.scand("mm/dd/yyyy",strStart);

//Calculation Pemformed

var oneDay = 24 * 60 * 60 * 1000;

             var dueMillis = dateStart.getTime() + 5 * oneDay;

             var dueDate = new Date(dueMillis);

//Date is printed

  event.value = util.printd("dd",dueDate);

}

This is like 99% of what I need to do.

However, I see how to figure out a future date, but how do I write the difference between the start date (StartDate) and the end of the year?

Also, I need to be able to print "Days Left" after the # of days is displayed.

This...

("" + dd + " days left",dueDate);

doesn't work in Acrobat...

Any ideas?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

You were on the right path before to calcluate the number of days to the end of the year...

This will give you the first day of the next year from dateStart:

var firstDayNextYear = util.scand("dd/mm/yyyy", "01/01/" + dateStart.getFullYear()+1);

Now you want to calculate the difference between them, right?

var diffMs = firstDayNextYear.getTime() - dateStart.getTime();

var diffDays = diffMs / oneDay;

And to apply it to the field where this is the calculation script you simply use:

event.value = diffDays + " days left";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

Here's the entire thing, the way we've worked it out so far... But it's not rendering the amount of days between the StartDate and the end of the year... Not sure where I went wrong... Ideas?

//Start 'Days Until' Script

var strStart = this.getField("StartDate").value;

    if(strStart.length) {

         var dateStart = util.scand("mm/dd/yyyy",strStart);

//Calculation Pemformed

var oneDay = 24 * 60 * 60 * 1000;

var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" +

dateStart.getFullYear()+1);

var diffMs = firstDayNextYear.getTime() - dateStart.getTime();

var diffDays = diffMs /

oneDay;

//Date is printed

event.value = diffDays + " days left";

}

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

Are there any error messages in the console? Does it produce any output at all?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

No... the output box doesn't produce anything. And it did before... I'm not sure where to go from here...     

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

Start debugging... Add console.println() commands to check the values of the various variables and make sure that they are what you expect them to be...

Also, you might want to use valueAsString in your first line instead of value.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

I started debugging and this is where I'm at right now...

getField("DATE") is null

1:Field:Calculate

TypeError: getField("DATE") is null

1:Field:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

I'm not sure I really understand this too well...     

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2013 Jan 30, 2013

There's not field in your file called "DATE"... Remember that JS is

case-sensitive, so if your field is actually called "date", you have to

write it that way.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

Okay... I made an adjustment and now I'm getting some output at least... Here's the code...

//Start 'Days Until' Script

var strStart = this.getField("StartDate").value;

    if(strStart.length) {

         var dateStart = util.scand("mm/dd/yyyy",strStart);

//Calculation Pemformed

var oneDay = 24 * 60 * 60 * 1000;

var firstDayNextYear = util.scand("mm/dd/yyyy", dateStart.setFullYear()+1);

var diffMs = firstDayNextYear.getTime() - dateStart.getTime();

var diffDays = diffMs /

oneDay;

//Date is printed

event.value = diffDays + " days left";

}

Now, it's displaying NaN days left in the output field.

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

firstDayNextYear is null

13:Field:Calculate

TypeError: firstDayNextYear is null

13:Field:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

getField("DATE") is null

1:AcroForm:PTO:Calculate

TypeError: getField("DATE") is null

1:AcroForm:PTO:Calculate

In the form PTO, I had the following:

DATE(YEAR(StartDate),12,31)-StartDate

I have lower-cased the "date" word and completely removed this line. The error still doesn't go away when I re-debug it. Any ideas?

And what do you think this line means?

13:Field:Calculate

TypeError: firstDayNextYear is null

I think we're getting somewhere, but not sure how to fix some of these things.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2013 Jan 31, 2013

You messed up the definition of firstDayNextYear. Go back to my code and

see how it should be done.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2013 Jan 31, 2013

Here is my code up to this point. You'll notice that down below, I have re-compiled the code and

the debugger is showing significantly less errors.

//Start 'Days Until' Script

var strStart = this.getField("StartDate").value;

    if(strStart.length) {

         var dateStart = util.scand("mm/dd/yyyy",strStart);

//Calculation Pemformed

var oneDay = 24 * 60 * 60 * 1000;

var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" + dateStart.getFullYear()+1);

var diffMs = firstDayNextYear.getTime() - dateStart.getTime();

var diffDays = diffMs /

oneDay;

//Date is printed

event.value = diffDays + " days left";

}

These "null" errors... I am not sure where to go with those.

And there's a "date is not defined" error, too.

firstDayNextYear is null

12:Field:Calculate

TypeError: firstDayNextYear is null

12:Field:Calculate

date is not defined

1:Field:Calculate

ReferenceError: date is not defined

1:Field:Calculate

Any ideas here?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2013 Jan 31, 2013

I re-wrote the code for you, and added some debugging commands:

event.value = "";

var strStart = this.getField("StartDate").valueAsString;

if(strStart.length) {         

    console.println("strStart:"+strStart);

    var dateStart = util.scand("mm/dd/yyyy",strStart);

    console.println("dateStart:"+dateStart);

    var oneDay = 24 * 60 * 60 * 1000; 

    var firstDayNextYear = util.scand("mm/dd/yyyy", "01/01/" + (dateStart.getFullYear()+1));

    console.println("firstDayNextYear:"+firstDayNextYear);

    var diffMs = firstDayNextYear.getTime() - dateStart.getTime();

    var diffDays = diffMs / oneDay;

    console.println("diffDays:"+diffDays);

    event.value = diffDays + " days left";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2013 Jan 31, 2013

Okay try67... We're really close now...

Here's the debugging info. The decimal place goes out about 14 places, but it's behaving properly.

firstDayNextYear is null

12:Field:Calculate

TypeError: firstDayNextYear is null

12:Field:Calculate

date is not defined

1:Field:Calculate

ReferenceError: date is not defined

1:Field:CalculatestrStart:03/01/2013

dateStart:Fri Mar 01 2013 00:00:00 GMT-0700 (US Mountain Standard Time)

firstDayNextYear:Wed Jan 01 2014 00:00:00 GMT-0700 (US Mountain Standard Time)

diffDays:306.00000001157406

date is not defined

1:AcroForm:PTO:Calculate

ReferenceError: date is not defined

1:AcroForm:PTO:CalculatestrStart:04/05/2013

dateStart:Fri Apr 05 2013 00:00:00 GMT-0700 (US Mountain Standard Time)

firstDayNextYear:Wed Jan 01 2014 00:00:00 GMT-0700 (US Mountain Standard Time)

diffDays:271.00000003472223

date is not defined

1:AcroForm:PTO:Calculate

ReferenceError: date is not defined

1:AcroForm:PTO:Calculate

I'm going to see what I can do now about the decimal places...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2013 Jan 31, 2013

Okay... So, I played around with the User Input part of the form a bit and put in different dates. If I put in a date where the days remaining in the year is a whole number, then it shows like 335 Days Left (I used 01/31/2013). But, when I put in tomorrow's date (02/01/2013), I get 334.00000001157406.

I think it might be something in the milliseconds calculation. What are your thoughts?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 31, 2013 Jan 31, 2013

Yes, probably... Change this:

var diffDays = diffMs / oneDay;

To:

var diffDays = Math.floor(diffMs / oneDay);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2013 Jan 31, 2013
LATEST

That worked. This is great!

Thanks for all your help with this. You've certainly taught me quite a bit about js in Acrobat. You're really good at this!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines