none
Powershell, format a date, after using .AddDays() RRS feed

  • Question

  • I seem to be in between a rock and a hard place.  

    I want to get a date that is three days earlier than today. 

    PS> $Temp=(Get-date).AddDays(-3)

    But I want to have this formatted in UFormat as "2015-03-15"

    (Get-Date -UFormat "%Y-%m-%d"

    I've tried every combination of the two, but keep getting errors. 

    Thanks. 

    --- Larry 

    Monday, May 4, 2015 6:54 PM

Answers

  • Hi Larry,

    This should do what you're after:

    $Temp = (Get-date).AddDays(-3)
    Get-Date $Temp -Format yyyy-MM-dd
    
    2015-05-01


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 7:01 PM
  • This works.

    (Get-Date).AddDays(-3) | Get-Date -UFormat "%Y-%m-%d"

    It uses Get-Date to get the date three days ago and then pipes that into another Get-Date which takes the date three days ago and formats it.
    • Edited by ThmsRynr Monday, May 4, 2015 7:05 PM added clarification
    • Proposed as answer by Mike Laughlin Monday, May 4, 2015 7:09 PM
    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 7:04 PM
  • (Get-Date).AddDays(-3).ToString('yyyy-MM-dd')

    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Monday, May 4, 2015 8:23 PM
    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 8:13 PM

All replies

  • Hi Larry,

    This should do what you're after:

    $Temp = (Get-date).AddDays(-3)
    Get-Date $Temp -Format yyyy-MM-dd
    
    2015-05-01


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 7:01 PM
  • This works.

    (Get-Date).AddDays(-3) | Get-Date -UFormat "%Y-%m-%d"

    It uses Get-Date to get the date three days ago and then pipes that into another Get-Date which takes the date three days ago and formats it.
    • Edited by ThmsRynr Monday, May 4, 2015 7:05 PM added clarification
    • Proposed as answer by Mike Laughlin Monday, May 4, 2015 7:09 PM
    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 7:04 PM
  • (Get-Date).AddDays(-3).ToString('yyyy-MM-dd')

    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Monday, May 4, 2015 8:23 PM
    • Marked as answer by lkeyes Monday, May 4, 2015 8:49 PM
    Monday, May 4, 2015 8:13 PM