Get time intervals between two Dates


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



Copy this code and paste it in your HTML
  1. /* Get time intervals between two Dates v2.0
  2.  *
  3.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License
  4.  * http://creativecommons.org/licenses/by/3.0/
  5.  *
  6.  * Author: Andy Harrison, http://dragonzreef.com/
  7.  * Date: 16 September 2011
  8.  */
  9.  
  10. Date.isLeapYear = function(year){ return (year%400 == 0 || (year%4 == 0 && year%100 != 0)); }
  11. Date.prototype.isLeapYear = function(){ return Date.isLeapYear(this.getFullYear()); }
  12.  
  13. //get the number of days in a given month of a given year (month being 0-11)
  14. Date.daysInMonth = function(year, month){ return ([31, (Date.isLeapYear(year)?29:28), 31,30,31,30,31,31,30,31,30,31])[month]; }
  15. Date.prototype.daysInMonth = function(){ return Date.daysInMonth(this.getFullYear(), this.getMonth()); }
  16.  
  17. //returns an object with time intervals between two Dates
  18. //Date objects, milliseconds, or date strings are accepted arguments
  19. //object properties: years, months, weeks, days, hours, minutes, seconds, milliseconds
  20. //each part is *in addition* to the sum of the larger parts; e.g., if there are 17 days between the two dates, .weeks==2 and .days==3
  21. Date.timeBetween = function(dateA, dateB)
  22. {
  23. dateA = new Date(dateA);
  24. dateB = new Date(dateB);
  25. if(isNaN(dateA.valueOf()+dateB.valueOf())) return null; //invalid date(s)
  26.  
  27. if(dateA.valueOf() > dateB.valueOf())
  28. {
  29. var tmp = dateA;
  30. dateA = dateB;
  31. dateB = tmp;
  32. }
  33.  
  34. var parts = {};
  35.  
  36. //years
  37. parts.years = dateB.getFullYear() - dateA.getFullYear();
  38.  
  39. //months
  40. dateA.setFullYear(dateB.getFullYear());
  41. parts.months = dateB.getMonth() - dateA.getMonth();
  42.  
  43. //days
  44. dateA.setMonth(dateB.getMonth());
  45. if(dateA.valueOf() > dateB.valueOf())
  46. {
  47. dateA.setMonth(dateA.getMonth()-1);
  48. parts.months--;
  49. }
  50. parts.days = Math.floor((dateB.valueOf()-dateA.valueOf())/86400000);
  51.  
  52. //time
  53. parts.hours = dateB.getHours() - dateA.getHours();
  54. parts.minutes = dateB.getMinutes() - dateA.getMinutes();
  55. parts.seconds = dateB.getSeconds() - dateA.getSeconds();
  56. parts.milliseconds = dateB.getMilliseconds() - dateA.getMilliseconds();
  57.  
  58. //weeks
  59. parts.weeks = Math.floor(parts.days/7);
  60. parts.days = parts.days%7;
  61.  
  62. //adjust for negative values
  63. if(parts.milliseconds < 0){ parts.milliseconds = 1000+parts.milliseconds; parts.seconds--; }
  64. if(parts.seconds < 0){ parts.seconds = 60+parts.seconds; parts.minutes--; }
  65. if(parts.minutes < 0){ parts.minutes = 60+parts.minutes; parts.hours--; }
  66. if(parts.hours < 0) parts.hours = 24+parts.hours;
  67. if(parts.months < 0){ parts.months = 12+parts.months; parts.years--; }
  68.  
  69. return parts;
  70. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.