none
Powershell: change default datetime format in script? RRS feed

  • Question

  • Hi,

    I have an html report in which I include dates.
    Now these dates appear in month/day/year. I want to prevent to need to change every date to format dd/mm/yyyy.
    Practical example in SCCM:(Get-CMDeployment -CollectionName $CollectionX).deploymenttime gives 11/15/2017 09:15:33
    whereas it should be 15/11/...

    Is there a way to set it in beginning of script so dates are in correct format?
    Tried
    Set-Culture -CultureInfo de-de but that does not work.

    Please advise.
    J.

    Jan Hoedt

    Thursday, November 30, 2017 4:35 PM

Answers

  • Produce culture specific output format:

    PS > [datetime]::Now.ToString([cultureinfo]::CreateSpecificCulture('en-US'))
    2/18/2018 11:05:12 AM
    PS > [datetime]::Now.ToString([cultureinfo]::CreateSpecificCulture('en-GB'))
    18/02/2018 11:05:18

    Available variations.

    PS > [datetime]::Now.ToString([cultureinfo]::CurrentUICulture)
    2/18/2018 11:06:44 AM
    PS > [datetime]::Now.ToString([cultureinfo]::DefaultThreadCurrentCulture)
    2/18/2018 11:06:54 AM
    PS > [datetime]::Now.ToString([cultureinfo]::DefaultThreadCurrentUICulture)
    2/18/2018 11:07:03 AM
    PS > [datetime]::Now.ToString([cultureinfo]::InstalledUICulture)
    2/18/2018 11:07:12 AM
    PS > [datetime]::Now.ToString([cultureinfo]::InvariantCulture)
    02/18/2018 11:07:25



    \_(ツ)_/

    Sunday, February 18, 2018 4:08 PM

All replies

  • If you are asking about DateTime objects, simply output in the format you want.

    For example:


    PS C:\> "{0:dd/MM/yyyy}" -f (Get-Date)
    30/11/2017
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, November 30, 2017 5:39 PM
    Moderator
  • hey,

    You can select from number of patterns( formats ) that includes in GetDateTimeFormats method for datetime type in PowerShell.

    You will find N number of patter on the link https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

    closes that I can go to your requirement was below command

    ((SCCM:(Get-CMDeployment -CollectionName $CollectionX).deploymenttime).getDateTimeFormats("d"))[7] 

    It result in format like -- 27-Oct-17

    • Proposed as answer by asharma5 Saturday, September 15, 2018 2:48 PM
    Saturday, December 2, 2017 12:23 PM
  • I know, but this doesn't change the value for the full script.

    Jan Hoedt

    Wednesday, December 13, 2017 2:54 PM
  • Your question is ambiguous. What do you mean by "change the value for the whole script"? A [DateTime] object is an object that contains a date and time, regardless of how you output it. If you want to output a [DateTime] in a particular format, nothing is stopping you from doing so.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, December 13, 2017 5:14 PM
    Moderator
  • I make html reports with Powershell.
    In thesee reports dates are in month/day/year instead of day/month/year. I would like to be date notation in day/month/year by default so I do not need to do manipulation/formatting on each date output.


    Jan Hoedt

    Thursday, December 14, 2017 9:22 AM
  • I had a similar issue and solved it like that:

    $cultureENUS = New-Object System.Globalization.CultureInfo("en-US")
    
    $myDate = (Get-Date).toString("MM/dd/yyyy HH:mm:ss", $cultureENUS)


    • Edited by TobyU Thursday, December 14, 2017 1:34 PM typo
    Thursday, December 14, 2017 1:34 PM
  • If you need your date in a particular format you simply specify your date in that particular format. You will have to do that for every single time you need it in your script but I assume you write this particular script only once. So what's the deal? If you've finished writing this script it will stay there for ever and will work as desinged. 

    Best regards,

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

    Thursday, December 14, 2017 2:15 PM
  • Use a function to output a DateTime in any format you want, then call the function in the script whenever you want that format.

    -- Bill Stewart [Bill_Stewart]

    Thursday, December 14, 2017 4:43 PM
    Moderator
  • I do not want to change date/time format each time. It's like I would tell you to change your keyboard settings, every time you logon, what's the deal?

    I want to change it one time only and have it in effect for all my scripts.


    Jan Hoedt

    Wednesday, January 10, 2018 1:22 PM
  • So there seems to be something wrong with your infrastructure. When I do

    (Get-CMDeployment -CollectionName 'CollName').deploymenttime

    I get just as expected dates in the normal German format.  ;-)

    You might check all related / dependend systems for faulty configurations.


    Best regards,

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


    • Edited by BOfH-666 Wednesday, January 10, 2018 1:51 PM
    Wednesday, January 10, 2018 1:51 PM
  • Thanks, my language on system is English, but date time should be not in English American format. So you suggest that if I would play with regional settings in OS, datetime should be also different in Powershell?

    Jan Hoedt

    Sunday, February 18, 2018 2:41 PM
  • What do you mean by "date time should not be in English American format"? There seems to be a fundamental misunderstanding here. A DateTime object doesn't have a "format." It stores a date and time. Formatting is only used when you want to output it as a string.

    You can experiment very easily at a command prompt. For example:


    PS C:\> ([DateTime]::Now).ToString()

    The documentation states that the ToString method "converts the value of the DateTime object to its equivalent string representation using the formatting conventions of the current culture."

    I will repeat what I said already earlier in this thread: If you want to output a DateTime object in a particular format, nothing is stopping you from doing so. Just output it in whatever format you want. If you want the same format for a bunch of different DateTime object outputs, write a short function that uses the format you want, and use the function.


    -- Bill Stewart [Bill_Stewart]

    Sunday, February 18, 2018 3:23 PM
    Moderator
  • Produce culture specific output format:

    PS > [datetime]::Now.ToString([cultureinfo]::CreateSpecificCulture('en-US'))
    2/18/2018 11:05:12 AM
    PS > [datetime]::Now.ToString([cultureinfo]::CreateSpecificCulture('en-GB'))
    18/02/2018 11:05:18

    Available variations.

    PS > [datetime]::Now.ToString([cultureinfo]::CurrentUICulture)
    2/18/2018 11:06:44 AM
    PS > [datetime]::Now.ToString([cultureinfo]::DefaultThreadCurrentCulture)
    2/18/2018 11:06:54 AM
    PS > [datetime]::Now.ToString([cultureinfo]::DefaultThreadCurrentUICulture)
    2/18/2018 11:07:03 AM
    PS > [datetime]::Now.ToString([cultureinfo]::InstalledUICulture)
    2/18/2018 11:07:12 AM
    PS > [datetime]::Now.ToString([cultureinfo]::InvariantCulture)
    02/18/2018 11:07:25



    \_(ツ)_/

    Sunday, February 18, 2018 4:08 PM
  • What jrv is pointing out, and what I have been trying to point out before, is that if you want to output a DateTime object in a specific format, then output it whatever format you want.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 19, 2018 3:54 PM
    Moderator
  • I see why the question is asked though. I'm new to powershell but there seem to be a number of cmdlets (e.g. export-csv) that do implicit date/time conversion under the hood and it would be good to change the default formatting options once. This would be a similar approach to some SQL scripts for example, that change a few defaults once.
    Monday, October 29, 2018 5:35 AM
  • I see why the question is asked though. I'm new to powershell but there seem to be a number of cmdlets (e.g. export-csv) that do implicit date/time conversion under the hood and it would be good to change the default formatting options once. This would be a similar approach to some SQL scripts for example, that change a few defaults once.

    Please do not ask your question n on another users topic.  It you have a question ask it by starting a new topic.


    \_(ツ)_/

    Monday, October 29, 2018 5:39 AM
  • Does this Help? https://stackoverflow.com/questions/22826185/setup-default-date-format-like-yyyy-mm-dd-in-powershell

    $currentThread = [System.Threading.Thread]::CurrentThread
    try { $culture = [CultureInfo]::InvariantCulture.Clone() } Catch { $culture = $CurrentThread.CurrentCulture.Clone() }
    $culture.DateTimeFormat.ShortDatePattern = 'yyyy-MM-dd'
    $culture.DateTimeFormat.ShortTimePattern = 'hh:mm tt'
    $currentThread.CurrentCulture = $culture
    $currentThread.CurrentUICulture = $culture

    Wednesday, November 14, 2018 8:29 PM