Skip to content Skip to sidebar Skip to footer

Javascript: I Have Subtracted Two Dates To Get The Difference Between Them, How Do I Find The Number Of Months Between The Two Dates?

Possible Duplicate: Difference in Months between two dates in JavaScript Just don't know how to get the difference in moths between two time or date objects. My current Code:

Solution 1:

Javascript Date object has several methods for this: http://www.w3schools.com/jsref/jsref_obj_date.asp if startTime and endTime are Date-objects:

var diff_in_Months = endTime.getMonth() - startTime.getMonth();

Taking years into account is bit more complicated.

var diff_in_Months = ( endTime.getFullYear()*12 + endTime.getMonth() ) - ( startTime.getFullYear()*12 + startTime.getMonth() );

I guess?

Post a Comment for "Javascript: I Have Subtracted Two Dates To Get The Difference Between Them, How Do I Find The Number Of Months Between The Two Dates?"