Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and custom time span format strings to display lapsed time.
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am awake and sipping a nice cup of English Breakfast tea with a little peppermint and spearmint leaves in it. I am thinking back over yesterday’s Hey, Scripting Guy! Blog post about Use PowerShell and Conditional Formatting to Format Time Spans, and I am a bit unhappy—oh, not about the blog—I thought it was pretty cool. I am unhappy about the limitation of the Standard Timespan Format Strings. They just are not that flexible.
Note This is the last blog post in a series of five that talk about using format methods and operators in Windows PowerShell.
- Understanding PowerShell and Basic String Formatting provided a great overview to the basics of using the Format method from the String class and the format operator from Windows PowerShell for composite formatting.
- Use PowerShell to Format Strings with Composite Formatting dove deeply into the use of composite formatting and using various format specifiers.
- Use PowerShell and Conditional Formatting to Format Numbers talked about using format specifiers to display percentages.
- Use PowerShell and Conditional Formatting to Format Dates discussed how to control the format of dates.
- Use PowerShell and Conditional Formatting to Format Time Spans explained how to use Windows PowerShell and conditional formatting to discover and format time spans.
So anyway, I thought I would take a look at creating a custom time span format string so I could display only the information I needed.
Note For complete documentation about these specifiers, see Standard TimeSpan Format Stringson MDSN. For complete information about composite formatting, see Composite Formatting.
Custom TimeSpan format strings
There is a great big table on MSDN that describes the custom TimeSpan format strings. For me, however, I only need to remember four. The four I like to use are shown in the following table.
Format Specifier | Description |
“dd” | Number of whole days in the time interval. Uses a leading zero as required. |
“hh” | Number of whole hours in the time interval (not including those that make up complete days). Therefore, an interval of 25 hours would report 01. To see the days, you would need to also use “dd”. |
“mm” | Number of whole minutes in the time interval (not including those that make up complete hours). Therefore, an interval of 65 hours would report 05. To see the hours, you would need to also use “hh”. |
“ss” | Number of whole seconds in the time interval (not including those that make up complete minutes). Therefore, an interval of 65 seconds would report 05. To see the minutes, you would need to also use “mm”. |
If I use a format specifier that is a single letter, such as “d”, I get the number of days without a leading zero. That, for me, makes sense; and therefore I do not need to remember four additional format specifiers—I simply know that they exist. The following script illustrates using a custom TimeSpan format string to display only the days that exist between two dates in a TimeSpan object. The nice thing about this technique is that I can design my output exactly in the way I wish, and then by using the format specifier.
PS C:\> $ts = [datetime]"3/14/13" -[datetime]"3/12/13"
PS C:\> "The lapsed time is: {0:dd} days" -f $ts
The lapsed time is: 02 days
Reusing the input object
One of the things that makes this technique so powerful is the ability to easily pull out the exact pieces from the TimeSpan object that I need to use in my output. It is much easier that using subexpressions to attempt to expand properties from the object. When I know how to use the appropriate format specifiers in the format item, I construct my string, and I select what I need to see. This makes for a much easier way of reading the time span than either the short or long time span formats that I presented in Use PowerShell and Conditional Formatting to Format Time Spans.
PS C:\> "The lapsed time is: {0:dd} days and {0:hh} hours" -f $ts
The lapsed time is: 02 days and 00 hours
I can continue to reuse the object and display days, hours, minutes, seconds, and even milliseconds if I need them. (Although in such a situation, I probably would go back and use the long format specifier “G” from the standard TimeSpan format strings. The following example illustrates the technique of picking out days, hours, and minutes from the TimeSpan object.
PS C:\> "The lapsed time is: {0:dd} days and {0:hh} hours and {0:mm} minutes" -f $ts
The lapsed time is: 02 days and 00 hours and 00 minutes
The previous script and its associated output are shown in the image that follows.
That is about it for today, and my tea is getting cold. Join me tomorrow when I will talk about using Windows PowerShell to configure a new laptop. It is a cool blog, and I’m sure you will enjoy it.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy