{"id":8241,"date":"2015-01-22T00:01:00","date_gmt":"2015-01-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/22\/formatting-date-strings-with-powershell\/"},"modified":"2019-02-18T10:35:51","modified_gmt":"2019-02-18T17:35:51","slug":"formatting-date-strings-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/formatting-date-strings-with-powershell\/","title":{"rendered":"Formatting Date Strings with PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about formatting date strings with Windows PowerShell.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. It seems that different ways to display dates and times are in great supply. In fact, there are dozens of ways to do this. If I decide that I do not like the way a date or time displays, I can change it. In addition, the way a particular date style displays in one country is different than the way it displays in another country.\nThese differences are part of the culture settings. For example, the output from the <b>Get-Date<\/b> cmdlet appears differently depending on the UI culture settings. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Use-Culture de-de {get-date}<\/p>\n<p style=\"margin-left:30px\">Mittwoch, 21. Januar 2015 12:23:40<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Get-Date<\/p>\n<p style=\"margin-left:30px\">Wednesday, January 21, 2015 12:23:45 PM\n<b>&nbsp; &nbsp;Note&nbsp;<\/b> The <b>Use-Culture<\/b> function is not standard in Windows PowerShell or Windows. It comes from Lee Holmes&rsquo; &nbsp;<br \/>&nbsp; &nbsp;PowerShell Cookbook module. I installed it from the <a href=\"http:\/\/msconfiggallery.cloudapp.net\/packages\/PowerShellCookbook\/1.2.0\" target=\"_blank\">PowerShell Gallery<\/a>, but it is also available on <a href=\"http:\/\/poshcode.org\/2226\" target=\"_blank\">PoshCode<\/a>.\nIn general, I want the date to automatically change the way it displays based on culture settings because it helps avoid confusion. There may be times when I want to override this behavior. I can do this by directly formatting the date. I can use the <b>ToString<\/b><i> <\/i>method and specify the display pattern. The pattern MM &ndash; dd &ndash;yyyy specifies that I want the month, day, and year to appear in that order. The MM is case sensitive. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; (Get-Date -Month 2 -Day 12 -Year 2015).tostring(&#8220;MM-dd-yyyy&#8221;)<\/p>\n<p style=\"margin-left:30px\">02-12-2015\nThere can be a very real problem with this technique. In many regions, the day value comes first. So is the month February or is it December? Here is how the date displays when I use German UI culture settings:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Use-Culture de-DE {(Get-Date -Month 2 -Day 12 -Year 2015).tostring(&#8220;MM-dd-yyyy&#8221;)}<\/p>\n<p style=\"margin-left:30px\">02-12-2015\nAccording to their pattern, this is December 2, 2015. Here is the short date pattern:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [System.Globalization.CultureInfo]::GetCultureInfo(1031).DateTimeFormat.ShortDatePattern<\/p>\n<p style=\"margin-left:30px\">dd.MM.yyyy\nNow the culture settings use a hexadecimal value from the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/goglobal\/bb896001.aspx\" target=\"_blank\">National Language Support API<\/a>. Germany uses a hex value of 0x0407. If I convert it to a decimal format, it becomes 1031. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; 0x0407<\/p>\n<p style=\"margin-left:30px\">1031\nI double check that I have the correct language settings by using the <b>GetCulturalInfo<\/b> static method:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [System.Globalization.CultureInfo]::GetCultureInfo(1031)<\/p>\n<p style=\"margin-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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8212;- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">1031&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; de-DE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; German (Germany)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nSo if I really want to display a short date, I need to use a format specifier to tell Windows PowerShell to display a short date. In this way, it will be culture specific. On the MSDN page, <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/az4se3k1(v=vs.110).aspx\">Standard Date and Time Format Strings<\/a>, I learn that the .NET short date specifier is <b>&ldquo;d&rdquo;<\/b>. So, I use the <b>&ndash;format<\/b> parameter of <b>Get-Date<\/b>, and the following appears:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date -Format d<\/p>\n<p style=\"margin-left:30px\">1\/21\/2015\nBut what about using a different culture? Well, this is how it displays in culture <b>de-DE<\/b>:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Use-Culture de-DE {get-date -Format d}<\/p>\n<p style=\"margin-left:30px\">21.01.2015\nAs a best practice, I should avoid creating my own date format strings if at all possible. If I use the built-in .NET date format specifiers, Windows PowerShell automatic displays it in the correct format. Here is a table of the .NET date format specifiers.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"73\" valign=\"top\">  <b>Format Specifier <\/b><\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p><b>Description <\/b><\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p><b>Examples<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>d<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Short date pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 6\/15\/2009 en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15\/06\/2009 fr-FR<\/p>\n<p>2009-06-15T13:45:30 -&gt; 2009\/06\/15 ja-JP<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>D<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Long date pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; Monday, June 15, 2009 en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15 &#1080;&#1102;&#1085;&#1103; 2009 &#1075;. ru-RU<\/p>\n<p>2009-06-15T13:45:30 -&gt; Montag, 15. Juni 2009 de-DE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>f<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Full date pattern with short time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; Monday, June 15, 2009 1:45 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; den 15 juni 2009 13:45 sv-SE<\/p>\n<p>2009-06-15T13:45:30 -&gt; &Delta;&epsilon;&upsilon;&tau;&#941;&rho;&alpha;, 15 &Iota;&omicron;&upsilon;&nu;&#943;&omicron;&upsilon; 2009 1:45 &mu;&mu; el-GR<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>F<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Full date pattern with long time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; Monday, June 15, 2009 1:45:30 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; den 15 juni 2009 13:45:30 sv-SE<\/p>\n<p>2009-06-15T13:45:30 -&gt; &Delta;&epsilon;&upsilon;&tau;&#941;&rho;&alpha;, 15 &Iota;&omicron;&upsilon;&nu;&#943;&omicron;&upsilon; 2009 1:45:30 &mu;&mu; el-GR<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>g<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>General date pattern with short time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 6\/15\/2009 1:45 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15\/06\/2009 13:45 es-ES<\/p>\n<p>2009-06-15T13:45:30 -&gt; 2009\/6\/15 13:45 zh-CN<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>G<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>General date pattern with long time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>&nbsp;2009-06-15T13:45:30 -&gt; 6\/15\/2009 1:45:30 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15\/06\/2009 13:45:30 es-ES<\/p>\n<p>2009-06-15T13:45:30 -&gt; 2009\/6\/15 13:45:30 zh-CN<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>M, m<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Month\/day pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; June 15 en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15. juni da-DK<\/p>\n<p>2009-06-15T13:45:30 -&gt; 15 Juni id-ID<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>O, o<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Round-trip date\/time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30&nbsp; &#8211;&gt; 2009-06-15T13:45:30.0000000-07:00<\/p>\n<p>2009-06-15T13:45:30 &#8211;&gt; 2009-06-15T13:45:30.0000000Z<\/p>\n<p>2009-06-15T13:45:30 2009-06-15T13:45:30.0000000<\/p>\n<p>2009-06-15T13:45:30-07:00 &#8211;&gt; 2009-06-15T13:45:30.0000000-07:00<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>R, r<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>RFC1123 pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; Mon, 15 Jun 2009 20:45:30 GMT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>s<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Sortable date\/time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 2009-06-15T13:45:30<\/p>\n<p>2009-06-15T13:45:30 -&gt; 2009-06-15T13:45:30<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>t<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Short time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 1:45 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 13:45 hr-HR<\/p>\n<p>2009-06-15T13:45:30 -&gt; 01:45 ar-EG<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>T<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Long time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 1:45:30 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; 13:45:30 hr-HR<\/p>\n<p>2009-06-15T13:45:30 -&gt; 01:45:30 &#1605; ar-EG<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>u<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Universal sortable date\/time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; 2009-06-15 20:45:30Z<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>U<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Universal full date\/time pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; Monday, June 15, 2009 8:45:30 PM en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; den 15 juni 2009 20:45:30 sv-SE<\/p>\n<p>2009-06-15T13:45:30 -&gt; &Delta;&epsilon;&upsilon;&tau;&#941;&rho;&alpha;, 15 &Iota;&omicron;&upsilon;&nu;&#943;&omicron;&upsilon; 2009 8:45:30 &mu;&mu; el-GR<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"73\" valign=\"top\">\n<p>Y, y<\/p>\n<\/td>\n<td width=\"144\" valign=\"top\">\n<p>Year month pattern<\/p>\n<\/td>\n<td width=\"421\" valign=\"top\">\n<p>2009-06-15T13:45:30 -&gt; June, 2009 en-US<\/p>\n<p>2009-06-15T13:45:30 -&gt; juni 2009 da-DK<\/p>\n<p>2009-06-15T13:45:30 -&gt; Juni 2009 id-ID<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That is all there is to using .NET date format specifiers. Date Time Week will continue tomorrow when I will talk about more cool stuff.\nI 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=\"http:\/\/blogs.technet.commailto: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><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about formatting date strings with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. It seems that different ways to display dates and times are in great supply. In fact, there are dozens of ways to do this. If I decide that I do not like the way [&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":[13,51,3,45],"class_list":["post-8241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-getting-started","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about formatting date strings with Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. It seems that different ways to display dates and times are in great supply. In fact, there are dozens of ways to do this. If I decide that I do not like the way [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8241","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=8241"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8241\/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=8241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}