javascript: I have subtracted two dates to get the difference between them, how do I find the number of months between the two dates? [duplicate]
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:
var start_time = plot_data[0][0]; // first array selector gets the pair, second: the time
var end_time = plot_data[plot_data.length - 1][0];
var time_range = end_time - start_time;
this would be nice:
var diff_in_months = time_range.months()?
=D
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?
精彩评论