How to get the months between 2 dates


I'm using jquery datepicker in order to get 2 dates. In my case I have to get the date in this format d-M-y. I'm using it in order to be able to insert the values into an oracle database without any formating. The problem is that I need to calculate the months between 2 dates. For example: 1-Oct-13 - 30-Oct-16.

Here is how I'm getting the dates in my js:

LastAssimDatePicker = (function() { $( "#lastAssimilationDate" ).datepicker({ yearRange: "-20:+100", changeMonth: true, changeYear: true, dateFormat: "d-M-y" }); }), LastAssimDateOverPicker = (function() { $( "#lastAssimilationDateOver" ).datepicker({ yearRange: "-20:+100", changeMonth: true, changeYear: true, dateFormat: "d-M-y" }); }),

The above dates are assigned to 2 variables - lastAssimilationDate and LastAssimDateOver

The problem is that I really can't find a way to calculate the months between the 2 dates and I already lost about 2 hours on it. I'm pretty sure that there will be an easy solution, but as a beginner, I'm not able to spot it. I know that there are simmilar topics here, but I can't get it working, or it does not fit on my issue.


Since you mentioned that you are not supposed to change the format I have a solution for you.

With the help of @T.J. Crowder's <a href="https://stackoverflow.com/a/2536445/1671639" rel="nofollow">Answer</a>, I done this

d1 = new Date($( "#lastAssimilationDate" ).val()); d2 = new Date($( "#lastAssimilationDateOver").val()); alert(monthDiff(d1, d2));

Check this <a href="http://jsfiddle.net/praveen_jegan/mB6B7/1/" rel="nofollow">JSFiddle</a>

Not sure Nan is popping, but I am able to see difference.

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/9dyBo.png" data-original="https://i.stack.imgur.com/9dyBo.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />


<strong>Working Demo</strong> <a href="http://jsfiddle.net/w54pq/" rel="nofollow">http://jsfiddle.net/w54pq/</a> <em>or</em> in case you want both month included try this demo: <a href="http://jsfiddle.net/yQyJq/" rel="nofollow">http://jsfiddle.net/yQyJq/</a>

This will fit your need, there can be many version for it.

Please note there can be various combinations with your need so feel free to play around with this code.

Old link one of which is my old reply:

<ul><li><a href="https://stackoverflow.com/questions/10931288/how-to-add-subtract-dates-with-javascript/10931362#10931362" rel="nofollow">How to add/subtract dates with javascript?</a></li> <li><a href="https://stackoverflow.com/questions/2536379/difference-in-months-between-two-dates-in-javascript" rel="nofollow">Difference in Months between two dates in JavaScript</a></li> </ul>

Hope this helps :)


$('.start,.end').datepicker(); $('.hulk').click(function () { var sDate = $('.start').val(); var nDate = $('.end').val(); var startdate = new Date(sDate); var enddate = new Date(nDate); enddate.setDate(enddate.getDate() - startdate.getDate()); alert(monthDiff(startdate,enddate)); }); function monthDiff(d1, d2) { var months; months = (d2.getFullYear() - d1.getFullYear()) * 12; months -= d1.getMonth() + 1; months += d2.getMonth(); return months <= 0 ? 0 : months; }

<strong>Working shot</strong>

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/OmkBP.png" data-original="https://i.stack.imgur.com/OmkBP.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />


function calcualteMonthYr(){ var fromDate =new Date($('#txtFrom').val()); //datepicker (text fields) var toDate = new Date($('#txtTo').val()); // datepicker var months; months = (toDate.getFullYear() - fromDate.getFullYear()) * 12; months += toDate.getMonth(); if (toDate.getDate() < fromDate.getDate()) { months--; } $('#txtTimePeriod2').val(months); } // it also consider dates so you would get exact months ..


  • Finding snake sequence in XY graph - Java
  • WCF, MySQL and Transaction
  • PHP - order multidimensional array in every possible way, maintining key-value association
  • values in multiple tables spring validation prob
  • Is there a point in collecting registration tokens when using only the GCM Topics?
  • making the background translucent
  • How do i use socket.select?
  • Keeping track of history in ghci
  • Kafka topic deletion not working
  • How to add dashes into a number input field while entering the number?
  • How to overload the calling process of any function
  • add scrollbar in qt window?
  • Sending notification from one app to another app in another device in Android
  • Opa: Iterating through stringmap and forming a new string based on it
  • Bootstrap fixed navigation jumps when scrolling
  • Android Studio 2.0 - this version of Android Studio is incompatible with the Gradle Plugin used. Try
  • Accessing music files into my application programatically
  • How to create a Unix-domain socket with specific access permissions
  • Using same constraints in multiple classes
  • Synchronize windows folders
  • C# - Most efficient way to iterate through multiple arrays/list
  • Getting the scrolling offset when storing coordinates
  • netsh acl setting (need alternative method - registry settings?)
  • Android changing fragment order inside FragmentPagerAdapter
  • Jquery popup on mouse over of calendar control
  • xtable package: Skipping some rows in the output
  • Remove final comma from string in vb.net
  • How do I get HTML corresponding to current DOM tree?
  • How to create a file in java without a extension
  • Fetching methods from BroadcastReceiver to update UI
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • htaccess rewriting URLs with multiple forward slashes
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • python regex in pyparsing
  • How to Embed XSL into XML