Skip to main content
jpcsolutions
Participating Frequently
January 30, 2013
Resuelto

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

  • January 30, 2013
  • 1 respuesta
  • 80420 visualizaciones

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);

}

Este tema ha sido cerrado para respuestas.
Mejor respuesta de try67

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?


Yes, probably... Change this:

var diffDays = diffMs / oneDay;

To:

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

1 respuesta

try67
Community Expert
Community Expert
January 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).

jpcsolutions
Participating Frequently
January 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?     

try67
Community Expert
Community Expert
January 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);