{"id":12901,"date":"2011-08-25T00:01:00","date_gmt":"2011-08-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/08\/25\/use-culture-information-in-powershell-to-format-dates\/"},"modified":"2011-08-25T00:01:00","modified_gmt":"2011-08-25T00:01:00","slug":"use-culture-information-in-powershell-to-format-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-culture-information-in-powershell-to-format-dates\/","title":{"rendered":"Use Culture Information in PowerShell to Format Dates"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use culture information in Windows PowerShell to format dates.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I enjoyed <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/08\/24\/use-powershell-to-convert-date-formats-easily.aspx\">your article yesterday<\/a> about manually changing the way a date is displayed using string techniques. But the problem is that is a bit complicated, and is even potentially error prone. Is there an easier way to work with dates and times in Windows PowerShell than manually creating string values in specific orders?<\/p>\n<p>&mdash;KL<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello KL,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The technique that allows for automatic conversion of date types is called <i>culture settings<\/i>. Whenever I think about culture, I remember Vienna, Austria. It seems the city&rsquo;s motto is &ldquo;Where business meets culture.&rdquo; I have taught several Windows PowerShell, VBScript, and WMI classes in Vienna over the years, and it is one of my favorite cities. Here is a photo I took of the Mozart statue during one of my last trips over.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6170.hsg-8-25-11-1.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Photo taken in Vienna, Austria\" alt=\"Photo taken in Vienna, Austria\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6170.hsg-8-25-11-1.jpg\" \/><\/a><\/p>\n<p>To me, the <b>Get-Culture <\/b>cmdlet will always be associated with Viennese classical music and long walks in the park that was near to our hotel.<\/p>\n<p>Whoops! It seems I have spoiled the surprise. To work automatically with dates, it is necessary to interact with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.globalization.cultureinfo.aspx\">CultureInfo<\/a> settings and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.datetime.aspx\">DateTime structure<\/a> from the .NET Framework. It is easy to convert a string into an instance of the <b>DateTime<\/b> structure. This is called <i>casting<\/i>, and to do this, I place the <b>DateTime<\/b> inside square brackets. The code that appears here converts a string into a <b>DateTime<\/b> object:<\/p>\n<p style=\"padding-left: 30px\">[datetime]&#8221;8\/25\/11&#8243;<\/p>\n<p>When converting a string into a <b>DateTime<\/b> object, the way the value displays depends on the current culture settings. I can use the <b>Get-Culture<\/b> cmdlet to determine my current culture settings. The command and associated output are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\Users\\ed.IAMMRED&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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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sp; en-US&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; English (United States)&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>By using the <b>Get-Member<\/b> cmdlet, I can see there is much more information available as well as a large number of methods that facilitate working with cultural settings. The <b>Get-Member<\/b> cmdlet (<b>gm<\/b> is the alias for the <b>Get-Member<\/b> cmdlet) and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.HSG-8-25-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Get-Member cmdlet and associated output\" alt=\"Image of Get-Member cmdlet and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0702.HSG-8-25-11-02.png\" \/><\/a><\/p>\n<p>The <b>DateTimeFormat<\/b> property that is returned from the <b>Get-Culture<\/b> cmdlet contains an object. This object is culture specific, and it contains <b>DateTimeFormat<\/b> information that corresponds to the cultural settings. Because the <b>DateTimeFormat<\/b> property returns an object, I can directly access by using a dotted notation:<\/p>\n<p style=\"padding-left: 30px\">(Get-culture).DateTimeFormat<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6471.HSG-8-25-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6471.HSG-8-25-11-03.png\" \/><\/a><\/p>\n<p>In the preceding figure, note that the <b>ShortDatePattern<\/b> <b>m\/d\/yyyy<\/b> is <b>Month\/Day\/Year<\/b>, which is the way dates are written in my cultural setting, <b>en-US<\/b>. The first two letters, <b>en<\/b>, correspond to English. The second two letters, <b>US<\/b>, correspond to the country. This is necessary because there are differences between English in the United States and in the United Kingdom (en-GB).<\/p>\n<p>Now for the really cool stuff. Anyone who has played around with the <b>Get-Date<\/b> cmdlet has seen the <i>format <\/i>parameter. I can use this parameter to configure the way that I want a date to display. For example, if I want day\/month\/two-digit year, I can use the command shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Date -Format &#8220;dd-MM-yy&#8221;<\/p>\n<p>By examining, the date patterns returned by the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.globalization.datetimeformatinfo.aspx\">DateTimeFormatInfo<\/a> object that comes back from the <b>DateTimeFormat<\/b> property, it dawned on me that I could use those patterns directly with the <i>format<\/i> parameter of the <b>Get-Date<\/b> cmdlet. This simplifies creating certain types of date outputs, and is more accurate as well because of the strong possibility of making a typo. For example, if I want to use the date in the <b>UniversalSortableDateTimePattern<\/b> format, I can use the command that is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Date -Format &#8220;yyyy&#8217;-&#8216;MM&#8217;-&#8216;dd HH&#8217;:&#8217;mm&#8217;:&#8217;ss&#8217;Z'&#8221;<\/p>\n<p>One problem is that this pattern appears somewhat cryptic. I can also directly use the <b>UniversalSortableDateTimePattern<\/b> property from the <b>DateTimeFormatInfo<\/b> object that is returned from the <b>Get-Culture<\/b> cmdlet. The <b>UniversalSortableDateTimePattern<\/b> property does not change from culture to culture, but other patterns, such as short date patterns, do change. Therefore, I can use the command that is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Date -Format (Get-culture).DateTimeFormat.UniversalSortableDateTimePattern&nbsp;<\/p>\n<p>Because I can easily create a new instance of a <b>CultureInfo<\/b> class by supplying either the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/goglobal\/bb896001.aspx\">culture ID (LCID)<\/a> or the culture name, that means I can convert the date output with ease, and with no hard-coding. In the code that follows, I create an instance of the German (Germany) <b>cultureInfo<\/b> class. I then use this class to display a short date in the German (Germany) pattern.<\/p>\n<p style=\"padding-left: 30px\">$de = New-Object system.globalization.cultureinfo(&#8220;de-DE&#8221;)<\/p>\n<p style=\"padding-left: 30px\">get-date -format ($de.DateTimeFormat.ShortDatePattern)<\/p>\n<p>&nbsp;After I know that I can create an instance of a specific <b>cultureInfo<\/b> class and use it to display a date in a particular format, I can write a script to display dates in a variety of outputs. The preferred way to create an instance of a <b>cultureInfo<\/b> class is to use the <b>string<\/b> method. The culture names are documented <a href=\"http:\/\/msdn.microsoft.com\/en-us\/goglobal\/bb896001.aspx\">on the same page where the LCIDs are located<\/a>. In the DisplayAlternateShortDates.ps1 script, I create an array of assorted culture string names. Next, I use the <b>foreach<\/b><i> <\/i>statement to walk through the array of culture names. Inside the <b>foreach<\/b><i> <\/i>loop, I use the <b>New-Object<\/b> cmdlet to create a new instance of a <b>cultureinfo<\/b> object. I pass the string name as an input argument to the cmdlet, and store the returned <b>cultureinfo<\/b> object in the <b>$culture<\/b> variable. Next, I use the <b>Get-Date<\/b> cmdlet to create the current date in the format specified by the <b>shortDatePattern<\/b> property. I then create a custom <b>psobject<\/b> that holds the culture name in a <b>name<\/b> property and the formatted short date in the <b>date<\/b> property. The complete DisplayAlternateShortDates.ps1 script s shown here:<i><\/i><\/p>\n<p style=\"padding-left: 30px\">$cultures = &#8220;en-US&#8221;,&#8221;en-GB&#8221;,&#8221;fr-CA&#8221;,&#8221;fr-FR&#8221;,&#8221;ms-MY&#8221;,&#8221;zh-HK&#8221;<\/p>\n<p style=\"padding-left: 30px\">foreach ($c in $cultures)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$culture = New-Object system.globalization.cultureinfo($c)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$date = get-date -format ($culture.DateTimeFormat.ShortDatePattern)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;New-Object psobject -Property @{&#8220;name&#8221;=$culture.displayname; &#8220;date&#8221;=$date}<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p>&nbsp;<\/p>\n<p>The script and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.HSG-8-25-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of script and associated output\" alt=\"Image of script and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.HSG-8-25-11-04.png\" \/><\/a><\/p>\n<p>I can use the static <b>GetCultures<\/b> method from the <b>globalization.cultureinfo<\/b> .NET Framework class to return culture names and IDs. If I want to see all the cultures, I use the <b>&ldquo;allcultures&rdquo;<\/b> enumeration. This command is shown here:<\/p>\n<p style=\"padding-left: 30px\">[globalization.cultureinfo]::GetCultures(&#8220;allCultures&#8221;)<\/p>\n<p>The output returns an object, and I can filter based upon the name. In the following command, I filter out all cultures that contain the cultures that begin with <b>fr<\/b><i> <\/i>by using the following command:<\/p>\n<p style=\"padding-left: 30px\">[globalization.cultureinfo]::GetCultures(&#8220;allCultures&#8221;) | where {$_.name -match &#8216;^fr&#8217;}&nbsp;<\/p>\n<p>By using this approach, I can display the date for all French cultures as shown here in the DisplayAlternateShortDatesFrench.ps1 script:<\/p>\n<p style=\"padding-left: 30px\">$cultures = [globalization.cultureinfo]::GetCultures(&#8220;allCultures&#8221;) |<\/p>\n<p style=\"padding-left: 30px\">&nbsp; where-object {$_.name -match &#8216;^fr&#8217;}<\/p>\n<p style=\"padding-left: 30px\">foreach ($c in $cultures)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$culture = New-Object system.globalization.cultureinfo($c)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$date = get-date -format ($culture.DateTimeFormat.ShortDatePattern)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;New-Object psobject -Property @{&#8220;name&#8221;=$culture.displayname; &#8220;date&#8221;=$date}<\/p>\n<p style=\"padding-left: 30px\">}&nbsp;<\/p>\n<p>The script and output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2234.HSG-8-25-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of script and associated output\" alt=\"Image of script and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2234.HSG-8-25-11-05.png\" \/><\/a><\/p>\n<p>KL that is all there is to using culture information to format dates. Join me tomorrow as I introduce a new Windows PowerShell superhero.<\/p>\n<p>&nbsp;<\/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\">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><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use culture information in Windows PowerShell to format dates. &nbsp; Hey, Scripting Guy! I enjoyed your article yesterday about manually changing the way a date is displayed using string techniques. But the problem is that is a bit complicated, and is even potentially error prone. Is there an easier way 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":[13,51,3,4,45],"class_list":["post-12901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use culture information in Windows PowerShell to format dates. &nbsp; Hey, Scripting Guy! I enjoyed your article yesterday about manually changing the way a date is displayed using string techniques. But the problem is that is a bit complicated, and is even potentially error prone. Is there an easier way to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12901","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=12901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12901\/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=12901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}