Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and conditional formatting to control the format of dates.
Hey, Scripting Guy! I know that Windows PowerShell has made it easier to deal with dates, especially with some of the tricks that are available via the Get-Date cmdlet. But at times I need to display different types of dates, and I end up doing old-fashioned string manipulation like I did back in the 70s. Now don’t get me wrong, I loved the 70s with the groovy music, polyester shirts, leisure suits, and big hair, but string manipulation was not part of that groovy scene.
—CG
Hello CG,
Microsoft Scripting Guy, Ed Wilson, is here. CG, thanks for the flashback! I just wonder if Microsoft PowerShell MVP, Sean Kearney, is thinking about spoofing Stayin’ Alive. Maybe something like, “Hey, hey, hey, scriptin’ it live”?
It is another beautiful day here in Charlotte, North Carolina. On Facebook, we are constantly seeing pictures of large piles of snow and the like, but we have not had more than a dusting here all winter. For us, it seems that winter is over, and spring is already knocking at the door. Some of our more obsessive neighbors have already pulled out their lawn mowers and are trying to make the rest of us look bad. Oh well. I am sitting here sipping a nice cup of Gunpowder green tea, with a bit of jasmine, crushed cinnamon stick, juniper berries, lemon grass, and a pinch of spearmint. It is wonderfully refreshing.
Note This is the fourth 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.
Understanding standard date and time format specifiers
The cool thing about using standard date and time format specifiers to format dates is that I do not need to worry if my script runs on another computer that may have a different culture format. By default, when I use a date and time format specifier, it automatically uses the culture of my local computer. In this way, the date and time information always displays correctly on whatever computer the script runs.
Note For complete documentation about these specifiers, see Standard Date and Time Format Stringson MDSN.
Use the Format parameter for Get-Date
The easiest way to use the standard date and time format specifiers is to use the –formatparameter from the Get-Date cmdlet. For example, if I need the date in a short form, such as 3/11/2013 (the month/day/year format that we commonly express in the United States), I use the “d” pattern. The “d” pattern is the short date pattern. This technique is shown here.
PS C:\> Get-Date -Format d
3/11/2013
Note The format specifiers are ALWAYS case sensitive. There are a few occasions when the format specifier appears as “M”, “m”. This means that I can use the upper case or the lower case “m”. In the case of “d”, the “d” format specifier is the short date pattern and “D” is the long date pattern. This pattern of short equals lower case and long equals upper case is commonly used. For example, “t” is the short time pattern and “T” is the long time pattern.
The following image illustrates the use of the standard date and time format specifiers with the Get-Date cmdlet.
Formatting strings
One thing to keep in mind is that when I specify a format for the output from Get-Date, I have converted my System.DateTime object to a string. At this point, I can no longer perform any date and time computations or conversions. These are strictly formatted output techniques. This does not mean that I cannot send the output to an Excel spreadsheet or a SQL Server database for further processing, but it is no longer a DateTime object.
In addition to specifying the format via Get-Date, I can also specify it via techniques that use conditional formatting. The following examples illustrate a few ways to accomplish this requirement.
Example 1: Use the Formatmethod from the String class
In Example 1, I use the static Formatmethod from the System.String .NET Framework class. In the Format item, I add in the first index, 0, which will populate via the Get-Date cmdlet. The format string is “R”, which creates an RFC1123 pattern.
PS C:\> [String]::Format("{0:R}",(get-date))
Mon, 11 Mar 2013 14:37:03 GMT
Example 2: Use the PowerShell format operator
I can use the Windows PowerShell Format (-f) operator to format the way the date displays information. On the left side of the –f operator, I place my format item with the applicable format specifier. On the right side of the –f operator, I call Get-Date. In the following technique, I use the Universal full date/time pattern.
PS C:\> "{0:U}" -f (Get-Date)
Monday, March 11, 2013 6:38:10 PM
Example 3: Show date in a specific culture
It is possible to create culture settings to permit displaying a date in a different format. The easiest way to do this is to use the New-Object cmdlet and create an instance of the Globalization.CultureInfo .NET Framework class. When I do this, I must specify the culture that I want to create. I can find the names of the cultures to create by referring to the National Language Support (NLS) API Reference.
Therefore, all I need to do after I create the new instance of the CultureInfo class is specify the style to display the date and specify the culture. In the following example, I get the current date and store it in a variable named $date. Next, I create the culture information for culture de-DE (German Germany) by using the New-Object cmdlet and specifying the culture name “de-DE”. Next, I use the WriteLinemethod to display the date as a string. I use the “D” format specifier to display a long date. Next, I specify the culture that I stored in the $ci variable. The output shows in German.
PS C:\> $date = get-date
PS C:\> $ci = New-Object globalization.cultureinfo("de-DE")
PS C:\> [console]::writeline($date.ToString("D",$ci))
Montag, 11. März 2013
However, when I use the Get-Culture cmdlet, I show that my current culture setting is English (United States).
PS C:\> Get-Culture
LCID Name DisplayName
---- ---- -----------
1033 en-US English (United States)
Note For excellent information about using culture settings to format dates, see the Hey, Scripting Guy! Blog, Use Culture Information in PowerShell to Format Dates.
CG, that is all there is to using Windows PowerShell to format dates. Format Week continues tomorrow when I will talk about using Windows PowerShell to format time spans.
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