Calculate Number of Months Between 2 Dates


/ Published in: Groovy
Save to your folder(s)

Calculates the number of Months between 2 given dates


Copy this code and paste it in your HTML
  1. def monthsBetween(from, to){
  2.  
  3. def cfrom = new GregorianCalendar(time:from)
  4. def cto = new GregorianCalendar(time:to)
  5.  
  6. return ((cto.get(Calendar.YEAR) - cfrom.get(Calendar.YEAR))
  7. * cto.getMaximum(Calendar.MONTH))
  8. + (cto.get(Calendar.MONTH) - cfrom.get(Calendar.MONTH))
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.