{"id":2138,"date":"2018-07-17T20:38:07","date_gmt":"2018-07-18T01:38:07","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2138"},"modified":"2018-07-17T20:38:07","modified_gmt":"2018-07-18T01:38:07","slug":"javascript-calculate-number-days-two-dates","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/17\/javascript-calculate-number-days-two-dates\/","title":{"rendered":"JavaScript: How to calculate the number of days between two dates"},"content":{"rendered":"<p>The following function takes in two dates and returns the number of days between them:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ Calculate the difference of two dates in total days\r\nfunction diffDays(d1, d2) {\r\n  let numDays = 0;\r\n  let tv1 = d1.getTime();  \/\/ msec since 1970\r\n  let tv2 = d2.getTime();\r\n\r\n  numDays = (tv2 - tv1) \/ 1000 \/ 86400;\r\n  numDays = Math.round(numDays - 0.5);\r\n  return numDays;\r\n}<\/pre>\n<p>This is very handy if you want to find the number of days that have elapsed since some day in the past, or if you want to calculate the number of days something is over due.\u00a0 First you set up the two dates that you are interested in. In this case, the second day is today.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let startDate = new Date('2017-01-20 11:00:00');  \/\/ 2000-01-01\r\nlet endDate =   new Date();                       \/\/ Today<\/pre>\n<p>Then you call the function like so:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let elapsedDays= diffDays(startDate, endDate);\r\nconsole.log(elapsedDays);<\/pre>\n<p>which yields 543.<\/p>\n<p>This function uses the getTime() method, which returns the numeric value associated with a UTC date. Getting the date as a numeric value is useful for these types of calculations, such as getting the number of days between two dates. This same method can be used to get the number of weeks, months, hours, seconds, etc..<\/p>\n<p>Here are some comments detailing how it works:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ This is the number of miliseconds since Jan 1, 1970, 00:00:00.000 GMT\r\nnumDays = (tv2 - tv1);\r\n\r\n\/\/ = 46945658190\r\n\r\n\/\/ Divide by 1000 to get seconds from miliseconds\r\nnumDays = (tv2 - tv1) \/ 1000;\r\n\r\n\/\/ = 46945658.19\r\n\r\n\r\n\/\/ Divide by 86400 (the number of seconds in a day) to get days\r\nnumDays = (tv2 - tv1) \/ 1000 \/ 86400;\r\n\r\n\/\/ = 543.3525842824074\r\n\r\n\/\/ Round the result\r\nnumDays = Math.round(numDays - 0.5);\r\n\r\n\/\/ = 543<\/pre>\n<p>For more information about the getTime() method, see:<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/getTime\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Date\/getTime<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_gettime.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_gettime.asp<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following function takes in two dates and returns the number of days between them: \/\/ Calculate the difference of two dates in total days function diffDays(d1, d2) { let numDays = 0; let tv1 = d1.getTime(); \/\/ msec since 1970 let tv2 = d2.getTime(); numDays = (tv2 &#8211; tv1) \/ 1000 \/ 86400; numDays &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/17\/javascript-calculate-number-days-two-dates\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to calculate the number of days between two dates<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[125,147,45,109],"class_list":["post-2138","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-date","tag-gettime","tag-javascript","tag-math-round"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2138"}],"version-history":[{"count":6,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2138\/revisions"}],"predecessor-version":[{"id":2144,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2138\/revisions\/2144"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}