locked
How to set correctly date short format? RRS feed

  • Question

  • For years, I've been using the following PowerShell code in my profile file to make sure I'm using ISO 8601 format for date.

    $culture = Get-Culture
    $culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
    Set-Culture $culture

    I've just installed PowerShell 7 and suddenly this code does not work anymore.  I got this error message:

       2 |  $culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
         |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         | Exception setting "ShortDatePattern": "Instance is read-only."

    So, what is the new and correct way of doing this in PowerShell 7?  Please.

    Thursday, April 16, 2020 3:33 PM

All replies

  • When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

    https://powershell.org/forums/topic/how-to-set-correctly-date-short-format/#post-219678

    Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, April 16, 2020 4:03 PM
  • A note on PS7.  It i not ready for primetime.  MS released it to get more real-world feedback.  Fundamental changes have been made to how Net Core handles many things.  I have found dozens of differences when using PowerShell but C# seems to use methods that don't fail since C# requires full construction of objects in many cases.

    To do this you will need to create a new cultureinfo object and set that as the active culture..  This should work in both PS7 and earlier versions of PS.

    The following appears to work in the released version of PS7 and PS 5.1

    $culture = Get-Culture
    $newculture = $culture.Clone()
    $newculture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
    Set-Culture $newculture


    \_(ツ)_/

    • Proposed as answer by BOfH-666 Thursday, April 16, 2020 11:22 PM
    • Unproposed as answer by Horinius Friday, April 17, 2020 8:22 PM
    Thursday, April 16, 2020 5:22 PM
  • A little inspection shows that the developers of Net Core forgot a few "Set" methods.  You might want to file a bug report on this with Net Core on GitHub.


    \_(ツ)_/

    Thursday, April 16, 2020 5:30 PM
  • Thanks, jrv.

    With due respect, especially for giving the code, I still have to protest.  I did NOT choose PS7 on purpose.  I just follow this page:

    https://www.thomasmaurer.ch/2019/03/how-to-install-and-update-powershell-6/

    and run that "one-liner" which installed PS7 instead of PS6.  Did I have a choice?

    About the code, I don't feel it's doing the same thing as mine.  Because if I run the following code, I don't have the expected result.

    Get-Date -DisplayHint Date -Format d

    You could check it by yourself.

    1. Set your short date format to, say, dd/MM/yyyy

    2. Then run the code suggested by you and then the "get-date" code in my example.  The expected result is 2020-04-18 (today's date) but you should get 18/04/2020 instead.

    About your suggestion to me to file a bug report.... I don't understand how to.  I have no experience on it.  And I don't even know what web site I should be going to report anything.

    Friday, April 17, 2020 8:34 PM
  • I answered the question you asked.  Now you are asking a new question that was not even implied in the first question.

    There are many culture dependent bits; input, output, console.  Each is controlled by a different setting.  Your output may also be controlled by the limits of some CmdLets.

    The following shows how to set the current culture globally

    $newculture = [cultureinfo]::CurrentCulture.Clone()
    $newculture.DateTimeFormat.ShortDatePattern = 'MM-yyyy-dd'
    [cultureinfo]::set_CurrentCulture($newculture)
    [cultureinfo]::CurrentCulture.DateTimeFormat.ShortDatePattern
    MM-yyyy-dd
    (Get-Date).ToShortDateString()
    04-2020-17

    Again - Win7 is not ready for primetime.


    \_(ツ)_/

    Friday, April 17, 2020 9:13 PM
  • Again - Win7 is not ready for primetime.

    PS7 !!  ;-) 

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, April 17, 2020 9:23 PM
  • PS 7.1 Preview is available but it still has too many bits bad or missing.

    Also no DSC or Workflow in PS7.

    How MS intends to add this is a mystery.  It is fundamental in a huge number of scrips on Windows and requires support at the language level.  It cannot be shoehorned in by a module.

    MS seems to be asleep on this.  My guess is that they are wooing the Unix people and the smartphone/tablet industry.


    \_(ツ)_/

    Friday, April 17, 2020 11:05 PM