{"id":235,"date":"2007-12-12T14:00:00","date_gmt":"2007-12-12T22:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/12\/12\/when-did-that-happen\/"},"modified":"2007-12-12T14:00:00","modified_gmt":"2007-12-12T22:00:00","slug":"when-did-that-happen","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/12\/12\/when-did-that-happen\/","title":{"rendered":"When Did That Happen?"},"content":{"rendered":"<p> VBScript has a useful function called <strong>DateDiff<\/strong> that will return a time interval between two dates.&nbsp; You can specify whether want the interval in months, days, minutes among other things. <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">d1=Now <br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">d2=CDate(&#8220;Jan 1, 2008 12:00AM&#8221;)<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8220;Countdown to the New Year (&#8221; &amp; d2 &amp; &#8220;)&#8221;<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8221; Months: &#8221; &amp; DateDiff(&#8220;m&#8221;,d1,d2)<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8221; Weeks: &#8221; &amp; DateDiff(&#8220;w&#8221;,d1,d2)<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8221; Days: &#8221; &amp; DateDiff(&#8220;d&#8221;,d1,d2)<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8221; Hours: &#8221; &amp; DateDiff(&#8220;h&#8221;,d1,d2)<br \/><\/font><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &#8221; Minutes: &#8221; &amp; DateDiff(&#8220;n&#8221;,d1,d2)<\/font> <\/p>\n<p>My problem is that I can never remember what abbreviation to use for the interval. In <a href=\"http:\/\/www.primalscript.com\/\" target=\"_blank\">PrimalScript 2007<\/a> this isn&#8217;t necessarily a big deal because I can use the help file from <a href=\"http:\/\/www.sapienpress.com\/WSHCore.asp\" target=\"_blank\">WSH and VBScript Core: TFM<\/a>. But maybe I can improve on this. How about a function that I can use in my script to return a date\/time interval that uses &#8220;real&#8221; English?<\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Function GetDateDiff(dtOne,dtTwo,sInterval)<br \/>&#8217; Valid interval values are:<br \/>&#8217; Months,Days,Minutes,Hours,Seconds and they <br \/>&#8217; are not case sensitive <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp; Select Case LCase(sInterval)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case &#8220;months&#8221; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = DateDiff(&#8220;M&#8221;,dtOne,dtNow) <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case &#8220;days&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = DateDiff(&#8220;d&#8221;,dtOne,dtNow) <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case &#8220;minutes&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = DateDiff(&#8220;n&#8221;,dtOne,dtNow) <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case &#8220;hours&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = DateDiff(&#8220;h&#8221;,dtOne,dtNow) <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case &#8220;seconds&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = DateDiff(&#8220;s&#8221;,dtOne,dtNow)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Case Else result=&#8221;Unknown interval&#8221; <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&nbsp;&nbsp;&nbsp; End Select<br \/>&nbsp;&nbsp;&nbsp; GetDateDiff=result <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">End Function<\/font> <\/p>\n<p>This function allows me to specify a date interval using a meaningful word like months or days. Now I don&#8217;t have to remember that &#8220;n&#8221; means minutes. I mean, who&#8217;s going to make that association? Using a function like this allows me to write more meaningful VBScript code:<\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">dtThen=#11\/28\/2007&nbsp; 11:41 AM#<br \/>dtNow=Now <\/font> <\/p>\n<p><font size=\"2\" face=\"Courier New\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo dtThen &amp; &#8221; was: &#8221; <br \/>WScript.Echo GetDateDiff(dtThen,dtNow,&#8221;months&#8221;) &amp;_<br \/> &#8221; months ago from &#8221; &amp; dtNow<br \/>WScript.Echo GetDateDiff(dtThen,dtNow,&#8221;days&#8221;) &amp;_<br \/> &#8221; days ago from &#8221; &amp; dtNow<br \/>WScript.Echo GetDateDiff(dtThen,dtNow,&#8221;hours&#8221;) &amp;_<br \/> &#8221; hours ago from &#8221; &amp; dtNow<br \/>WScript.Echo GetDateDiff(dtThen,dtNow,&#8221;minutes&#8221;) &amp;_<br \/> &#8221; minutes ago from &#8221; &amp; dtNow<br \/>WScript.Echo GetDateDiff(dtThen,dtNow,&#8221;seconds&#8221;) &amp;_<br \/> &#8221; seconds ago from &#8221; &amp; dtNow<\/font> <\/p>\n<p>Now I don&#8217;t have to rely on middle-aged memory and the code is a little more meaningful. I haven&#8217;t wrapped all the functionality from <strong>DateDiff<\/strong> into my function so if there is something you need, you&#8217;ll need to modify it. But this should meet 95% or more of my needs.<\/p>\n<p>I&#8217;ve attached the function and sample code as a text file so you don&#8217;t have to try and copy\/paste from the blog entry.<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:10a985d0-eb4e-4a17-8cc8-e5c8aa29f6a3\" style=\"margin: 0px; padding: 0px; display: inline;\">Technorati  Tags: <a href=\"http:\/\/technorati.com\/tags\/VBScript\" rel=\"tag\">VBScript<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Functions\" rel=\"tag\">Functions<\/a><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>VBScript has a useful function called DateDiff that will return a time interval between two dates.  You can specify whether want the interval in months, days, minutes among other things. My problem is that I can never remember what abbreviation to use for the interval. In PrimalScript 2007 this isn&#8217;t necessarily a big deal because I can use the help file from WSH and VBScript Core: TFM. But maybe I can improve on this. How about a function that I can use in my script to return a date\/time interval that uses &#8220;real&#8221; English?<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}