0

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

Community
  • 1
  • 1
DerNalia
  • 123
  • 2
  • 10
  • http://stackoverflow.com/questions/327429/whats-the-best-way-to-calculate-date-difference-in-javascript – zod Aug 17 '11 at 16:19
  • few helpful links: http://ditio.net/2010/05/02/javascript-date-difference-calculation/ http://www.javascriptkit.com/javatutors/datedifference.shtml – zod Aug 17 '11 at 16:19

1 Answers1

4

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?

AP-Green
  • 84
  • 3