Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating months 2

Status
Not open for further replies.

TheConeHead

Programmer
Joined
Aug 14, 2002
Messages
2,106
Location
US
I have two date objects. I need to determines how many months are between these two dates. Meaning if it was may 31 and June 1, I would get back 2 or if it was December 5 2006 and June 23 2007 - I would get back 7.

How can this be done?

[conehead]
 
Hmmm, my issue breaks down to the fact that getTime() won't due me any good (I don't think...) since given May 31 and June 1 - I want 2 returned and breaking it down to ms will just show me two days... and not two months... thanks, though...

[conehead]
 
Do a little adaptation, mate

Code:
<script type="text/javascript">

//Set the two dates
today=new Date()
var christmas=new Date(today.getFullYear(), 11, 25) //Month is 0-11 in JavaScript
if (today.getMonth()==11 && today.getDate()>25) //if Christmas has passed already
christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas
//Set 1 day in milliseconds
var one_month=1000*60*60*24*30

//Calculate difference btw the two dates, and convert to months
document.write(Math.ceil((christmas.getTime()-today.getTime())/(one_month))+
" moths left until Christmas!")

</script>

Cheers,
Dian
 
Thanks - I will give it a try...

[conehead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top