none
Powershell Get-date minus one day...

    Question

  • Here is the delima, I need to get yesterdays day in the format of yymmdd.  That's easy if you want todays date:

         get-date -uformat %y%m%d

    Great!  Now how do I get yesterdays date?

         (get-date).AddDays(-1)

    will get me

         Wednesday, June 22, 2011 8:47:51 AM

    But, I cant do

         (get-date).AddDays(-1) -uFormat %y%m%d

    Nor can I do

         (get-date -uFormat %y%m%d).AddDays(-1)

    Nor is there a .Net date/time format of yymmdd.  Any suggestions would be appreciated.  Thank you!

    Thursday, June 23, 2011 3:51 PM

Answers

  • Hi,

    "{0:yyyyMMdd}" -f (get-date).AddDays(-1)

    or

    (get-date).AddDays(-1).ToString("yyyMMdd")

    HTH,

    Bill

    • Edited by Bill_StewartModerator Thursday, June 23, 2011 4:00 PM Added another alternative
    • Marked as answer by shurley02 Thursday, June 23, 2011 4:02 PM
    Thursday, June 23, 2011 3:57 PM
    Moderator

All replies

  • Hi,

    "{0:yyyyMMdd}" -f (get-date).AddDays(-1)

    or

    (get-date).AddDays(-1).ToString("yyyMMdd")

    HTH,

    Bill

    • Edited by Bill_StewartModerator Thursday, June 23, 2011 4:00 PM Added another alternative
    • Marked as answer by shurley02 Thursday, June 23, 2011 4:02 PM
    Thursday, June 23, 2011 3:57 PM
    Moderator
  • You so rock AbqBill!!!!  Thank you, exactally what I needed!

     

    Shurley

    Thursday, June 23, 2011 4:03 PM
  • So if i wanted to get the current date to a format of YYYY.MM.DD, I can get it by

    (get-date).ToString("YYYYMMDD") ??

    Since i have file names that contain the below i need only to get those with a certain current date embedded.

    Update_YYYYMMDDHHMMSSMS.log or Update_2014021802113801.log

    Friday, February 21, 2014 9:58 AM
  • Hi,

    This question is already marked as answered. Please start a new question, and be as specific as possible about what you want to do.

    Bill

    Friday, February 21, 2014 10:29 AM
    Moderator
  • Perfect I used this in a string:

    –Date ("{0:dd/MM/yyyy}" -f (get-date).AddDays(-1))

    Works a treat :)

    • Proposed as answer by Franky Siu Monday, May 05, 2014 7:22 AM
    Wednesday, April 02, 2014 9:26 AM