{"id":4022,"date":"2013-03-15T00:01:00","date_gmt":"2013-03-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/15\/use-powershell-and-conditional-formatting-to-format-time-spans\/"},"modified":"2013-03-15T00:01:00","modified_gmt":"2013-03-15T00:01:00","slug":"use-powershell-and-conditional-formatting-to-format-time-spans","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-conditional-formatting-to-format-time-spans\/","title":{"rendered":"Use PowerShell and Conditional Formatting to Format Time Spans"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Learn how to use Windows PowerShell and conditional formatting to discover and &nbsp;format time spans.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! What is a time span, and why would I want to use one? Just sayin&#8217;&hellip;<\/p>\n<p>&mdash;JR<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JR,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning is one of those mornings that remind me of when I was growing up in Florida. The sky is overcast, and a ground fog clothes the lawn. The air is cool, but heavy with humidity. I am positive I hear sea gull calls pierce the thick silence, yet I have not seen a sea gull in months. Maybe I am not certain that I am in Charlotte, North Carolina after all. I am sipping a cup of Darjeeling tea with cinnamon stick, lemon grass, peppermint, spearmint, licorice root, and juniper berries, and munching on a freshly baked strawberry scone while I check my <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> email.<\/p>\n<p>JR, a time span measures the amount of time that is elapsing from one point in time to another point in time. Therefore, when figuring out the amount of time between March 14, 2013 and March 12, 2013, a <strong>TimeSpan<\/strong> object returns as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [datetime]&#8221;3\/14\/13&#8243; -[datetime]&#8221;3\/12\/13&#8243;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1728000000000<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 48<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2880<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 172800<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 172800000<\/p>\n<p><strong>Note&nbsp;&nbsp;&nbsp;<\/strong>This is the last blog post in a series of five that talk about using format methods and operators in Windows PowerShell.<\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/understanding-powershell-and-basic-string-formatting\/\" target=\"_blank\">Understanding PowerShell and Basic String Formatting<\/a> provided a great overview to the basics of using the&nbsp;<strong>Format<\/strong><em>&nbsp;<\/em>method from the&nbsp;<strong>String<\/strong>&nbsp;class and the&nbsp;<strong>format<\/strong>&nbsp;operator from Windows PowerShell for composite formatting.<\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/controlpanel\/blogs\/posteditor.aspx?SelectedNavItem=Posts&amp;WeblogID=7618&amp;WeblogPostID=3557525\" target=\"_blank\">Use PowerShell to Format Strings with Composite Formatting<\/a> dove deeply into the use of composite formatting and using various format specifiers.<\/li>\n<li><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-conditional-formatting-to-format-numbers\/\" target=\"_blank\">Use PowerShell and Conditional Formatting to Format Numbers<\/a> talked about using format specifiers to display percentages.<\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2013\/03\/14\/use-powershell-and-conditional-formatting-to-format-dates.aspx\" target=\"_blank\">Use PowerShell and Conditional Formatting to Format Dates<\/a> discussed how to control the format of dates.<\/li>\n<\/ul>\n<h2>The problem with the default display of a TimeSpan object<\/h2>\n<p>By default, Windows PowerShell displays all of the properties associated with a <strong>TimeSpan<\/strong> object. As seen in the previous script, the output can be a bit cluttered. This is OK if I am working interactively from within the Windows PowerShell console and I only want to see something like the amount of time that has lapsed between two dates.<\/p>\n<p>I can see the different properties when I send the <strong>TimeSpan<\/strong> object to the <strong>Get-Member<\/strong> cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">[datetime]&#8221;3\/14\/13&#8243; -[datetime]&#8221;3\/12\/13&#8243; | Get-Member -MemberType *property<\/p>\n<p>The command and the output associated with the command are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5428.HSG-3-15-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5428.HSG-3-15-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>So, if I only want to know the number of hours between two date\/time objects, I need to first find out the property name (by using <strong>Get-Member<\/strong> or by simply running the command and examining the output). Then I can use the group and dot method to retrieve the specific property. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ([datetime]&#8221;3\/14\/13&#8243; -[datetime]&#8221;3\/12\/13&#8243;).totalhours<\/p>\n<p style=\"padding-left: 30px\">48<\/p>\n<p>In general, I use this technique when I am interested in a specific property from a <strong>TimeSpan<\/strong> object. But there is a better (and much more flexible) way to obtain exactly what I want from a <strong>TimeSpan<\/strong> object.<\/p>\n<h2>Use composite formatting and standard TimeSpan format strings<\/h2>\n<p>The easiest way to use the standard TimeSpan format strings is to combine them with composite formatting. When I do this, it provides three formats. The formats are shown in the following table.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p><strong>Format Specifier<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p><strong>Name<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; &ldquo;c&rdquo;<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; Constant format (not culture sensitive.<\/p>\n<p>&nbsp; [-]d.hh:mm:ss.[fffffff]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; &ldquo;g&rdquo;<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; General short format (culture sensitive)<\/p>\n<p>&nbsp; [-]d.hh:mm:ss.[fffffff]<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; &ldquo;G&rdquo;<\/p>\n<\/td>\n<td valign=\"top\" width=\"319\">\n<p>&nbsp; General long format (culture sensitive)<\/p>\n<p>&nbsp; [-]d.:hh:mm:ss.fffffff<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;&nbsp;&nbsp;<\/strong>For complete documentation about these specifiers, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee372286.aspx\" target=\"_blank\">Standard TimeSpan Format Strings<\/a><em> <\/em>on MDSN.<\/p>\n<p>An example of using the three standard TimeSpan format strings with composite formatting is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7612.HSG-3-15-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7612.HSG-3-15-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p><strong>Note&nbsp;&nbsp;&nbsp;<\/strong>For complete information about composite formatting, see&nbsp;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/txafckwd.aspx\" target=\"_blank\">Composite Formatting<\/a><em>&nbsp;<\/em>on<em>&nbsp;<\/em>MSDN.<\/p>\n<h3>Example 1: Use the static WriteLine<em> <\/em>method<\/h3>\n<p>To use standard TimeSpan format strings with the static <strong>WriteLine<\/strong><em> <\/em>method from the system.console .NET Framework class, I use a string to identify the value I display, and then I use a format item with the &ldquo;c&rdquo; format specifier. The &ldquo;c&rdquo; format specifier is a constant value that is not cultural sensitive. Therefore, the values always appear in the same order. The second parameter that the <strong>WriteLine<\/strong><em> <\/em>method accepts is a <strong>TimeSpan<\/strong> object. The script and its associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $ts = [datetime]&#8221;3\/14\/13&#8243; -[datetime]&#8221;3\/12\/13&#8243;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; [console]::WriteLine(&#8220;The lapsed time is: {0:c}&#8221;, $ts)<\/p>\n<p style=\"padding-left: 30px\">The lapsed time is: 2.00:00:00<\/p>\n<h3>Example 2: Use the Windows PowerShell format operator<\/h3>\n<p>To use standard TimeSpan format strings with the Windows PowerShell format operator, I add a string descriptor, and then use my format item to specify the substitution object and the format to use on the left side of the format operator. On the right side of the format operator, I specify the variable holding the <strong>TimeSpan<\/strong> object. The script is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $ts = [datetime]&#8221;3\/14\/13&#8243; -[datetime]&#8221;3\/12\/13&#8243;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; &#8220;The lapsed time is: {0:g}&#8221; -f $ts<\/p>\n<p style=\"padding-left: 30px\">The lapsed time is: 2:0:00:00<\/p>\n<h3>Example 3: Time span with milliseconds<\/h3>\n<p>One of the things that is useful with the &ldquo;c&rdquo; and the &ldquo;g&rdquo; standard TimeSpan format strings is that they display the portion of the <strong>TimeSpan<\/strong> object that is required. Therefore, if a time span does not span days, it omits the days portion. If a <strong>TimeSpan<\/strong> object populates milliseconds, it adds that portion of the object.<\/p>\n<p>In the script that follows, I store the current <strong>DateTime<\/strong> object. I wait for a while, and then I store a second <strong>DateTime<\/strong> object. Now I create a <strong>TimeSpan<\/strong> object representing the difference between the two <strong>TimeSpan<\/strong> objects. Lastly, I use the Windows PowerShell format operator to display a culture sensitive short time span. Because both times occurred on the same day, the day portion of the object does not display.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $now = get-date<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $later = get-date<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $nts = New-TimeSpan -Start $now -End $later<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; &#8220;The timespan is {0:g}&#8221; -f $nts<\/p>\n<p style=\"padding-left: 30px\">The time span is 0:09:03.1051821<\/p>\n<p>This technique is shown here, and it illustrates the difference between the three standard time span format strings.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7532.HSG-3-15-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7532.HSG-3-15-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>JR, that is all there is to using a time span. Join me tomorrow for the Weekend Scripter when I will talk about using a custom time span format specifier.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell and conditional formatting to discover and &nbsp;format time spans. &nbsp;Hey, Scripting Guy! What is a time span, and why would I want to use one? Just sayin&#8217;&hellip; &mdash;JR &nbsp;Hello JR, Microsoft Scripting Guy, Ed Wilson, is here. This morning is one of those mornings that remind me of [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[416,3,4,45],"class_list":["post-4022","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-formatting-output","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell and conditional formatting to discover and &nbsp;format time spans. &nbsp;Hey, Scripting Guy! What is a time span, and why would I want to use one? Just sayin&#8217;&hellip; &mdash;JR &nbsp;Hello JR, Microsoft Scripting Guy, Ed Wilson, is here. This morning is one of those mornings that remind me of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4022","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4022"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4022\/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=4022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}