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

util.scand and valueof

alexpapa
Registered: Nov 25 2008
Posts: 68
Answered

Hi everyone,

Has anyone else encountered the problem where valueOf on a date/time in milliseconds does not return consistent values?

i.e. I quite extensively have to calculate the number of days between two dates,

so I would use something like:

date1 = util.scand("mm/dd/yyyy" + "HH:MM:ss", month+"/"+day+"/"+year + "00:00:00");
var dateVal = date1.valueOf();

Now if I print out the value of dateVal with the same input, it is not always the same value. I have stipulated the minutes, seconds and hours to always get midnight on the day in question, but it still does not return the same value.

Is this because of milliseconds as well, and if so, how is it deciding that value?

This makes it complicated when comparing dates, for example, when subtracting one date from another I can't just say if date1 is less than date 2 - because it may not be less, but still actually be one day behind..

if that makes sense? :)

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you do not need the fractional seconds, you can truncated them from the value.

// get the date objectdate1 = util.scand("mm/dd/yyyy" + "HH:MM:ss", month + "/" + day + "/" + year + "00:00:00");// display the date objectconsole.println('date1: ' + date1);// convert to a numbervar dateVal = date1.valueOf();console.println('dateVal: ' + dateVal);// truncate to nearest seconddateVal = Math.floor(date1.valueOf() / 1000) * 1000;console.println('truncated dateVal: ' + dateVal);// convert milliseconds to date objectvar oDate2 = new Date(dateVal);console.println('Truncated date object: ' + oDate2);

George Kaiser