开发者

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]

This question already has answers here: Closed 11 years ago.

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜