{"id":4026,"date":"2013-03-14T00:01:00","date_gmt":"2013-03-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/14\/use-powershell-and-conditional-formatting-to-format-dates\/"},"modified":"2013-03-14T00:01:00","modified_gmt":"2013-03-14T00:01:00","slug":"use-powershell-and-conditional-formatting-to-format-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-conditional-formatting-to-format-dates\/","title":{"rendered":"Use PowerShell and Conditional Formatting to Format Dates"},"content":{"rendered":"<p><strong style=\"font-size:12px\">Summary<\/strong><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and conditional formatting to control the format of dates.<\/span><\/p>\n<p><img decoding=\"async\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" \/>&nbsp;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 <strong>Get-Date<\/strong> 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&rsquo;t get me wrong, I loved the 70s with the groovy music, polyester shirts, <a href=\"http:\/\/www.bing.com\/images\/search?q=70%27S+Leisure+Suits&amp;FORM=RESTAB\" target=\"_blank\">leisure suits<\/a>, and big hair, but string manipulation was not part of that groovy scene.<\/p>\n<p>&mdash;CG<\/p>\n<p><img decoding=\"async\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" \/>&nbsp;Hello CG,<\/p>\n<p>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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Stayin%27_Alive\" target=\"_blank\">Stayin&rsquo; Alive<\/a>. Maybe something like, &ldquo;Hey, hey, hey, scriptin&rsquo; it live&rdquo;?<\/p>\n<p>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.<\/p>\n<p style=\"padding-left:30px\"><strong>Note&nbsp;&nbsp;&nbsp;<\/strong>This is the fourth 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=\"https:\/\/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=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-format-strings-with-composite-formatting\/\">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=\"https:\/\/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<\/ul>\n<h2>Understanding standard date and time format specifiers<\/h2>\n<p>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.<\/p>\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\/az4se3k1.aspx\" target=\"_blank\">Standard Date and Time Format Strings<\/a><em> <\/em>on MDSN.<\/p>\n<h2>Use the Format parameter for Get-Date<\/h2>\n<p>The easiest way to use the standard date and time format specifiers is to use the <strong>&ndash;format<\/strong><em> <\/em>parameter from the <strong>Get-Date<\/strong> 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 &ldquo;d&rdquo; pattern. The &ldquo;d&rdquo; pattern is the short date pattern. This technique is shown here.<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; Get-Date -Format d<\/p>\n<p style=\"padding-left:30px\">3\/11\/2013<\/p>\n<p style=\"padding-left:30px\"><strong>Note&nbsp;&nbsp;<\/strong>&nbsp;The format specifiers are ALWAYS case sensitive. There are a few occasions when the format specifier appears as &ldquo;M&rdquo;, &ldquo;m&rdquo;. This means that I can use the upper case or the lower case &ldquo;m&rdquo;. In the case of &ldquo;d&rdquo;, the &ldquo;d&rdquo; format specifier is the short date pattern and &ldquo;D&rdquo; is the long date pattern. This pattern of short equals lower case and long equals upper case is commonly used. For example, &ldquo;t&rdquo; is the short time pattern and &ldquo;T&rdquo; is the long time pattern.<\/p>\n<p>The following image illustrates the use of the standard date and time format specifiers with the <strong>Get-Date<\/strong> cmdlet.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5466.HSG-3-14-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" alt=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5466.HSG-3-14-13-01.png\" \/><\/a><\/p>\n<h2>Formatting strings<\/h2>\n<p>One thing to keep in mind is that when I specify a format for the output from <strong>Get-Date<\/strong>, 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.<\/p>\n<p>In addition to specifying the format via <strong>Get-Date<\/strong>, I can also specify it via techniques that use conditional formatting. The following examples illustrate a few ways to accomplish this requirement.<\/p>\n<h3>Example 1: Use the Format<em> <\/em>method from the String class<\/h3>\n<p>In Example 1, I use the static <strong>Format<\/strong><em> <\/em>method from the System.String .NET Framework class. In the <strong>Format<\/strong> item, I add in the first index, <strong>0<\/strong>, which will populate via the <strong>Get-Date <\/strong>cmdlet. The format string is &ldquo;R&rdquo;, which creates an RFC1123 pattern.<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; [String]::Format(&quot;{0:R}&quot;,(get-date))<\/p>\n<p style=\"padding-left:30px\">Mon, 11 Mar 2013 14:37:03 GMT<\/p>\n<h3>Example 2: Use the PowerShell format operator<\/h3>\n<p>I can use the Windows PowerShell <strong>Format<\/strong> (<strong>-f<\/strong>) operator to format the way the date displays information. On the left side of the <strong>&ndash;f<\/strong> operator, I place my format item with the applicable format specifier. On the right side of the <strong>&ndash;f<\/strong> operator, I call <strong>Get-Date<\/strong>. In the following technique, I use the Universal full date\/time pattern.<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; &quot;{0:U}&quot; -f (Get-Date)<\/p>\n<p style=\"padding-left:30px\">Monday, March 11, 2013 6:38:10 PM<\/p>\n<h3>Example 3: Show date in a specific culture<\/h3>\n<p>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 <strong>New-Object<\/strong> 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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/goglobal\/bb896001.aspx\" target=\"_blank\">National Language Support (NLS) API Reference<\/a><em>. <\/em><\/p>\n<p>Therefore, all I need to do after I create the new instance of the <strong>CultureInfo<\/strong> 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 <strong>$date<\/strong>. Next, I create the culture information for culture de-DE (German Germany) by using the <strong>New-Object<\/strong> cmdlet and specifying the culture name &ldquo;de-DE&rdquo;. Next, I use the <strong>WriteLine<\/strong><em> <\/em>method to display the date as a string. I use the &ldquo;D&rdquo; format specifier to display a long date. Next, I specify the culture that I stored in the <strong>$ci<\/strong> variable. The output shows in German.<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; $date = get-date<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; $ci = New-Object globalization.cultureinfo(&quot;de-DE&quot;)<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; [console]::writeline($date.ToString(&quot;D&quot;,$ci))<\/p>\n<p style=\"padding-left:30px\">Montag, 11. M&auml;rz 2013<\/p>\n<p>However, when I use the <strong>Get-Culture<\/strong> cmdlet, I show that my current culture setting is English (United States).<\/p>\n<p style=\"padding-left:30px\">PS C:\\&gt; Get-Culture<\/p>\n<p style=\"padding-left:30px\">&nbsp;<\/p>\n<p style=\"padding-left:30px\">LCID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DisplayName<\/p>\n<p style=\"padding-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left:30px\">1033&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; en-US&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; English (United States)<\/p>\n<p style=\"padding-left:30px\"><strong>Note<\/strong> &nbsp;&nbsp;For excellent information about using culture settings to format dates, see the Hey, Scripting Guy! Blog, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-culture-information-in-powershell-to-format-dates\/\" target=\"_blank\">Use Culture Information in PowerShell to Format Dates<\/a>.<em> <\/em><\/p>\n<p>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.<\/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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and conditional formatting to control the format of dates. &nbsp;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 [&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-4026","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: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and conditional formatting to control the format of dates. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4026","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=4026"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4026\/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=4026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}