{"id":70313,"date":"2005-03-03T13:33:00","date_gmt":"2005-03-03T13:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/03\/how-can-i-determine-the-elapsed-time-between-two-dates\/"},"modified":"2005-03-03T13:33:00","modified_gmt":"2005-03-03T13:33:00","slug":"how-can-i-determine-the-elapsed-time-between-two-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-elapsed-time-between-two-dates\/","title":{"rendered":"How Can I Determine the Elapsed Time Between Two Dates?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I tell how many days there are between two dates? For example, if I have a log file that says an application started on January 1, 2005 and that the application ended today, is there a way to determine how many days the application ran?<BR><BR>&#8212; AL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AL. You know, in the second <I>Hey, Scripting Guy!<\/I> column ever written (<A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/aug04\/hey0803.mspx\">&#8220;How Can I Get Yesterday&#8217;s Date?&#8221;<\/A>), we talked about simple date arithmetic. For some reason, though, we didn\u2019t talk about <I>this<\/I> kind of date arithmetic, the ability to determine elapsed time between two dates. So let\u2019s rectify that right now.<\/P>\n<P>As it turns out, doing date arithmetic such as this is remarkably easy with VBScript; that\u2019s because VBScript includes a <B>DateDiff<\/B> function designed to carry out calculations such as this. Want to know the number of days between today and January 1, 2005? Then just use a script like this one:<\/P><PRE class=\"codeSample\">dtmEndingDate = Date\nintDays = DateDiff(&#8220;d&#8221;, #1\/1\/2005#, dtmEndingDate)\nWscript.Echo intDays\n<\/PRE>\n<P>Yep, that\u2019s it: just three lines of code. We begin by assigning today\u2019s date (<B>Date<\/B>) to a variable named dtmEndingDate; that\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">dtmEndingDate = Date\n<\/PRE>\n<P>We then call the DateDiff function and store the results in a variable named intDays; that\u2019s what <I>this<\/I> line of code does:<\/P><PRE class=\"codeSample\">intDays = DateDiff(&#8220;d&#8221;, #1\/1\/2005#, dtmEndingDate)\n<\/PRE>\n<P>Note that DateDiff requires three parameters. First, we need to indicate the time interval we\u2019re using. For this script we\u2019re using days, so we pass the parameter <B>\u201cd\u201d<\/B>. We could also use time intervals such as hours (\u201ch\u201d), weeks (\u201cw\u201d), or months (\u201cm\u201d). For a complete list, see <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_vbs_xvyf.mspx\">&#8220;Working with Dates and Times&#8221;<\/A> in the Microsoft Windows 2000 Scripting Guide.<\/P>\n<P>Second, we need to indicate the starting date (in this case, 1\/1\/2005); note that we surround the date using pound signs (#) to ensure that VBScript interprets the value as a date. <\/P>\n<P>Finally, we indicate the ending date. VBScript will calculate the number of days between the two dates and stash that number in the variable intDays. At that point all we have to do is to echo back the elapsed time.<\/P>\n<P>As we noted, you can use different time intervals; you\u2019re not limited to just days. For example, suppose you know that your application started at 8:00 AM on January 1, 2005 and ended at 9:30 PM on January 5, 2005. Want to know how many <I>hours<\/I> the application was running? Here\u2019s a script that does just that:<\/P><PRE class=\"codeSample\">intHours = DateDiff _\n    (&#8220;h&#8221;, #1\/1\/2005 8:00 AM#, #1\/5\/2005 9:30 PM#)\nWscript.Echo intHours\n<\/PRE>\n<P>Note that the value returned will be rounded to the nearest hour. To get even more specific, you could calculate the elapsed time in minutes and then divide by 60 in order to determine hours and fraction of an hour. But we\u2019ll leave that up to you.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell how many days there are between two dates? For example, if I have a log file that says an application started on January 1, 2005 and that the application ended today, is there a way to determine how many days the application ran?&#8212; AL Hey, AL. You know, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,3,4,5],"class_list":["post-70313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell how many days there are between two dates? For example, if I have a log file that says an application started on January 1, 2005 and that the application ended today, is there a way to determine how many days the application ran?&#8212; AL Hey, AL. You know, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70313\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}