Friday, November 13, 2009

Wierd date difference calculaton in javascript?

Hi,





I'm getting a wierd date difference between a user chosen date and the current date. Code below shows a difference between 2/2/08 and today is 30 days. I know I'm missing something obvious.


Does anybody know what I'm doing wrong?


Thanks for the help.


Dave


----------------


%26lt;HTML%26gt;


%26lt;BODY%26gt;


%26lt;SCRIPT%26gt;


var dtBirthDate = new Date()


dtBirthDate.setMonth(2)


dtBirthDate.setDate(2)


dtBirthDate.setFullYear(2008)


dtBirthDate.setHours(0)


dtBirthDate.setMinutes(0)


dtBirthDate.setSeconds(0)


var iBirthDateMs = Date.parse(dtBirthDate.toUTCString())





var dtCurrentDate = new Date()


var iCurrentDateMs = dtCurrentDate.getTime()


document.write("birthdate ms " + iBirthDateMs + " current day ms" + iCurrentDateMs + "%26lt;BR%26gt;")





var iBirthDateDiffMs = iBirthDateMs - iCurrentDateMs


document.write("diff ms " + iBirthDateDiffMs + "%26lt;BR%26gt;")





var iBirthDateDiffDays = Math.floor(iBirthDateDiffMs/86400000)


document.write("it is " + iBirthDateDiffDays + "

Wierd date difference calculaton in javascript?
Actually, the setMonth method calculates months from 0 to 11, with 0 being January and 11, December. So, if you use dtBirthDate.setMonth(1), your results should be correct.





You may find my notes here helpful:





http://www.dougv.com/blog/2006/12/11/fun...
Reply:document.write("it is " + iBirthDateDiffDays + "





try closing the end of the string with a ) and see what you get....


i'm just too familiar with javascript, infact i'm learning java right now. So if i sound like in idea forgive me. Just double check the small things, i.e. declared variables, concatenations, etc)


No comments:

Post a Comment