locked
Get Month name RRS feed

  • Question

  • Hi,

    I have a problem when I try to get the month day. For June I didn't have any problem but for this month it is not good.

    $CurrentMonth = (Get-Date).month
    $Current = (Get-Culture).DateTimeFormat.GetMonthName($CurrentMonth)
    $Today = Get-Date -Format "dd-$Current-yyyy"
    $Today

    The result is:

    02-Jul20-2020 -> I don't understand why I have Jul20, for last month the result is 02-June-2020.

    I have tested all months and for a lot of them it is not working.

    Have you any idea of this problem? How can I fix it?

    Thank you.

    T.

    Thursday, July 2, 2020 9:03 AM

Answers

  • And why are you not using

    Get-Date -Format 'dd-MMMM-yyyy'
    ???  ;-)


    Live long and prosper!

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

    • Proposed as answer by Vector BCO Thursday, July 2, 2020 12:58 PM
    • Marked as answer by Adrian.Tom Monday, July 13, 2020 11:58 AM
    Thursday, July 2, 2020 10:30 AM

All replies

  • And why are you not using

    Get-Date -Format 'dd-MMMM-yyyy'
    ???  ;-)


    Live long and prosper!

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

    • Proposed as answer by Vector BCO Thursday, July 2, 2020 12:58 PM
    • Marked as answer by Adrian.Tom Monday, July 13, 2020 11:58 AM
    Thursday, July 2, 2020 10:30 AM
  • Get month simple.

    [datetime]::Now.ToString('MMMM')

    No matter what language or locale this always works.

    For formatting all of a date use BOfH-666 method which is related.


    \_(ツ)_/

    Thursday, July 2, 2020 4:12 PM
  • See here:

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.8

    Your $Current variable contains the value "July". Of those letters only "y" is recognized as a format, and that produces the year from 0 to 99. So "J", "u", and "l" are copied to the result string and "y" produces the value "20".

    Last month ("June") none of the letters matched any of the formats so the characters are copied to the result string unchanged.

    BOfH-666gave you the way to get the results you're looking for.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by BOfH-666 Thursday, July 2, 2020 6:51 PM
    Thursday, July 2, 2020 6:34 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Yang Yang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, July 3, 2020 3:02 AM
  • OMG! So easy!

    Thanks.

    Monday, July 13, 2020 11:58 AM
  • Hi,

    I am glad to hear that your issue was successfully resolved.   :)

    If there is anything else we can do for you, please feel free to post in the forum.

    Best Regards,

    Young Yang


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Tuesday, July 14, 2020 1:27 AM