date formats

  • mtz676

    SSCrazy Eights

    Points: 8704

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

  • Sue_H

    SSC Guru

    Points: 89729

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Try something along the lines of:
    $SomeDate = (Get-date).AddMonths(1)
    Get-Date $SomeDate -Format 'Y'

    Sue

  • blakemcneill

    SSC Journeyman

    Points: 87

    PowerShell uses the .NET format for date and time. You can look at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings to see how t to use the build in standard formats. If you need something more custom, use this: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings.

  • Jeff Moden

    SSC Guru

    Points: 993661

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Ummm... use T-SQL instead? 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • Sue_H

    SSC Guru

    Points: 89729

    Jeff Moden - Friday, February 22, 2019 11:32 AM

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Ummm... use T-SQL instead? 😀

    Odd as it may seem, a lot of times when someone posts a Powershell question in the Powershell forum they are often looking for an answer in Powershell. 
    Imagine that 🙂

  • ZZartin

    SSC-Dedicated

    Points: 30242

    Jeff Moden - Friday, February 22, 2019 11:32 AM

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Ummm... use T-SQL instead? 😀

    Powershell has better date formatting options than CONVERT 😛

  • Jeff Moden

    SSC Guru

    Points: 993661

    ZZartin - Friday, February 22, 2019 11:56 AM

    Jeff Moden - Friday, February 22, 2019 11:32 AM

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Ummm... use T-SQL instead? 😀

    Powershell has better date formatting options than CONVERT 😛

    "Better" how?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • Jeff Moden

    SSC Guru

    Points: 993661

    Sue_H - Friday, February 22, 2019 11:52 AM

    Jeff Moden - Friday, February 22, 2019 11:32 AM

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Ummm... use T-SQL instead? 😀

    Odd as it may seem, a lot of times when someone posts a Powershell question in the Powershell forum they are often looking for an answer in Powershell. 
    Imagine that 🙂

    Heh... I've been finding that, odd as it may seem, when someone posts a Powershell question in the Powershell forum, it's because they don't actually know enough about T-SQL nor how easy things can be there. 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code problems

  • David Burrows

    SSC Guru

    Points: 64396

    Sue_H - Wednesday, July 25, 2018 10:44 AM

    mtz676 - Wednesday, July 25, 2018 4:09 AM

    ((Get-Date).AddMonths(1))
    gives me
    Saturday, August 25, 2018 3:06:26 AM

    (Get-Date -format 'Y')
    gives me
    July, 2018

    How do  i get August, 2018 when I run code  in the month of July with the output in this format
    August, 2018

    Thanks

    Try something along the lines of:
    $SomeDate = (Get-date).AddMonths(1)
    Get-Date $SomeDate -Format 'Y'

    Sue

    Or

    (Get-Date).AddMonths(1).ToString("MMMM, yyyy")

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply