{"id":9611,"date":"2006-09-17T15:39:00","date_gmt":"2006-09-17T15:39:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/09\/17\/useful-datetime-functions-librarytime-ps1\/"},"modified":"2019-02-18T13:21:19","modified_gmt":"2019-02-18T20:21:19","slug":"useful-datetime-functions-librarytime-ps1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/useful-datetime-functions-librarytime-ps1\/","title":{"rendered":"Useful DateTime functions &#8211; LibraryTime.ps1"},"content":{"rendered":"<p>I was going to post a blog entries and realized that the example I wanted to give used some of my utility functions so I&#8217;ll share them first.&nbsp; I hope you find them interesting.&nbsp; I put them in a file called LibraryTime.ps1 and dot source that during my login.&nbsp; BTW&nbsp; &#8211; everyone should adopt that pattern.&nbsp; Files that contain functions meant to be dot sourced into another script should be called LibraryXXXX.ps1<\/p>\n<p><font face=\"Courier New\" size=\"1\">function <strong>Measure-TimeSpan<\/strong>([TimeSpan]$Span, $Units=$Null)<br \/>{<br \/>&nbsp;&nbsp; switch ($Units)<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp; &#8220;Weeks&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[Int]($Span.TotalDays \/ 7)}<br \/>&nbsp;&nbsp; &#8220;Days&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[Int]$Span.TotalDays}<br \/>&nbsp;&nbsp; &#8220;Hours&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[Int]$Span.TotalHours}<br \/>&nbsp;&nbsp; &#8220;Minutes&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[Int]$Span.TotalMinutes}<br \/>&nbsp;&nbsp; &#8220;Seconds&#8221;&nbsp;&nbsp;&nbsp; &nbsp; {[Int]$Span.TotalSeconds}<br \/>&nbsp;&nbsp; &#8220;MilliSeconds&#8221; {[Int64]$Span.TotalMilliSeconds}<br \/>&nbsp;&nbsp; &#8220;Ticks&#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {$Span.Ticks}<br \/>&nbsp;&nbsp; default&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {$null}<br \/>&nbsp;&nbsp; }<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">function <strong>Measure-Since<\/strong>([DateTime]$Time, $Units=$Null)<br \/>{<br \/>&nbsp;&nbsp; $Since = Measure-TimeSpan $([DateTime]::Now &#8211; $Time) $Units<br \/>&nbsp;&nbsp; if ($Since -eq $null)<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw &#8216;Usage: Measure-Since -Time &lt;DateTime&gt; -Units [&#8220;Weeks&#8221;,&#8221;Days&#8221;,&#8221;Hours&#8221;,&#8221;Minutes&#8221;,&#8221;Seconds&#8221;,&#8221;Milliseconds&#8221;,&#8221;Ticks&#8221;]&#8217;<br \/>&nbsp;&nbsp; }<br \/>&nbsp;&nbsp; else<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Since<br \/>&nbsp;&nbsp; }<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">function <strong>Measure-Till<\/strong>([DateTime]$Time, $Units=$Null)<br \/>{<br \/>&nbsp;&nbsp; $Since = Measure-TimeSpan $($Time &#8211; [DateTime]::Now) $Units<br \/>&nbsp;&nbsp; if ($Since -eq $null)<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw &#8216;Usage: Measure-Till -Time &lt;DateTime&gt; -Units [&#8220;Weeks&#8221;,&#8221;Days&#8221;,&#8221;Hours&#8221;,&#8221;Minutes&#8221;,&#8221;Seconds&#8221;,&#8221;Milliseconds&#8221;,&#8221;Ticks&#8221;]&#8217;<br \/>&nbsp;&nbsp; }<br \/>&nbsp;&nbsp; else<br \/>&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Since<br \/>&nbsp;&nbsp; }<br \/>}<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Set-Alias MSince Measure-Since<br \/>Set-Alias MTill&nbsp; Measure-Till<br \/><\/font><\/p>\n<p>Now let&#8217;s try them out:<\/p>\n<p>&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">&#8220;weeks&#8221;,&#8221;Days&#8221;,&#8221;Hours&#8221;,&#8221;Seconds&#8221;,&#8221;Milliseconds&#8221;,&#8221;ticks&#8221; |&nbsp;<br \/><\/font><\/strong>&gt;&gt;&nbsp;&nbsp;&nbsp; <\/font><font face=\"Courier New\" size=\"1\"><strong><font color=\"#000080\">%{ &#8220;{0,16} $_&#8221; -f (Measure-Since &#8220;12\/25\/2005&#8221; $_)}<br \/><\/font><\/strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 38 weeks<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 267 Days<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6398 Hours<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 23034210 Seconds<br \/>&nbsp;&nbsp;&nbsp;&nbsp; 23034209798 Milliseconds<br \/>&nbsp;230342098115960 ticks<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">&#8220;weeks&#8221;,&#8221;Days&#8221;,&#8221;Hours&#8221;,&#8221;Seconds&#8221;,&#8221;Milliseconds&#8221;,&#8221;ticks&#8221; | <br \/><\/font><\/strong>&gt;&gt;&nbsp;&nbsp;&nbsp; <font color=\"#000080\"><strong>%{ &#8220;{0,16} $_&#8221; -f (Measure-Till &#8220;12\/25\/2006&#8221; $_)}<br \/><\/strong><\/font><strong><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 14 weeks<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 98 Days<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2362 Hours<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8501789 Seconds<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8501788713 Milliseconds<br \/>&nbsp; 85017886963120 ticks<\/font><\/strong><\/font><\/p>\n<p>Enjoy<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p><font face=\"Courier New\" size=\"1\"><\/font>&nbsp;<\/p>\n<p><font face=\"Courier New\" size=\"1\">PSMDTAG:DOTNET Datetime<\/font><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/75\/96\/95\/LibraryTime.ps1\">LibraryTime.ps1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was going to post a blog entries and realized that the example I wanted to give used some of my utility functions so I&#8217;ll share them first.&nbsp; I hope you find them interesting.&nbsp; I put them in a file called LibraryTime.ps1 and dot source that during my login.&nbsp; BTW&nbsp; &#8211; everyone should adopt that [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[14,59,60,61],"class_list":["post-9611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-dotnet","tag-measure-since","tag-measure-till","tag-measure-timespan"],"acf":[],"blog_post_summary":"<p>I was going to post a blog entries and realized that the example I wanted to give used some of my utility functions so I&#8217;ll share them first.&nbsp; I hope you find them interesting.&nbsp; I put them in a file called LibraryTime.ps1 and dot source that during my login.&nbsp; BTW&nbsp; &#8211; everyone should adopt that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9611"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}