none
Need help in formatting Date and Time! RRS feed

  • Question

  • Hello Experts,

    Need your help in something what I am doing.

    A quick Brief:

    I am trying to write a PS Script which will callup a legacy executable and send two dates as parameters - Its a custom EXE which will accept the date ONLY in below format

    29/05/2015 (DD/MM/YYYY)

    So I have to pass the parameters as Today's date and 75 days from today. Ie.,  29/05/2015 and 12/08/2015.

    I am able to get the today's date and give it to execuatble using the snippet below

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

    This is What I need: Add 75 days from today and get in the same above format.

    Any help in this would be very much appreciated.

    Friday, May 29, 2015 1:28 PM

Answers

  • / vs. - may be realted to your selected language and the date seperator?

    (Get-Culture).DateTimeFormat

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:13 PM
    Friday, May 29, 2015 2:45 PM
  • We can force culture:

    [datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy',[System.Globalization.CultureInfo]'en-US')


    \_(ツ)_/

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:05 PM
    Friday, May 29, 2015 2:51 PM
  • [datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy')

    \_(ツ)_/

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:04 PM
    Friday, May 29, 2015 2:19 PM
  • PowerShell -f operator can format a date as a string in any format you want.


    PS C:\> "{0:MM/dd/yyyy}" -f (get-date "12/8/2015")
    12/08/2015
    

    See Custom Date and Time Format Strings in the documentation.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:04 PM
    Friday, May 29, 2015 2:30 PM
    Moderator

All replies

  • This work probably work - 

    ((Get-Date).AddDays(75)).ToShortDateString()

    Friday, May 29, 2015 1:42 PM
  • Thanks so much for your quick reply Braham

    However the command you gave returns "12-08-2015" but my exe doesn't take in this format.

    In other words, I need slashes instead of Hyphen something like 12/08/2015.

    Do we have any way to convert date in this format?

    Friday, May 29, 2015 1:50 PM
  • [datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy')

    \_(ツ)_/

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:04 PM
    Friday, May 29, 2015 2:19 PM
  • PowerShell -f operator can format a date as a string in any format you want.


    PS C:\> "{0:MM/dd/yyyy}" -f (get-date "12/8/2015")
    12/08/2015
    

    See Custom Date and Time Format Strings in the documentation.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:04 PM
    Friday, May 29, 2015 2:30 PM
    Moderator
  • No Luck JRV :-( , still getting dashes.

    Friday, May 29, 2015 2:32 PM
  • Odd.  I get the slashes as expected.

    You could do a replace if you continue to get the dashes.

    Friday, May 29, 2015 2:39 PM
  • No Luck JRV :-( , still getting dashes.

    I don't think so. What are you running? XP?

    \_(ツ)_/

    Friday, May 29, 2015 2:42 PM
  • / vs. - may be realted to your selected language and the date seperator?

    (Get-Culture).DateTimeFormat

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:13 PM
    Friday, May 29, 2015 2:45 PM
  • / vs. - may be realted to your selected language and the date seperator?

    (Get-Culture).DateTimeFormat

    String formatter bypasses the culture.

    08()12/2015
    PS C:\scripts> [datetime]::ToDay.AddDays(75).ToString('MM]dd]yyyy')
    08]12]2015
    PS C:\scripts> [datetime]::ToDay.AddDays(75).ToString('MM//dd//yyyy')
    08//12//2015
    PS C:\scripts>

    Corrupt system or missing patches.


    \_(ツ)_/

    Friday, May 29, 2015 2:48 PM
  • We can force culture:

    [datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy',[System.Globalization.CultureInfo]'en-US')


    \_(ツ)_/

    • Marked as answer by Vasu Deva Friday, May 29, 2015 3:05 PM
    Friday, May 29, 2015 2:51 PM
  • Guys, just now noticed some thing very weird. I am getting "-" as seperator when I run on my local Windows 7. But when I run the same on my Server 2008 R2 Server I get "/" as seperator.

    From Windows7

    From Windows 2008 R2

    Friday, May 29, 2015 2:56 PM
  • Thanks for your excellent help JRV, Bill, Jalapeno42 ! YOU GUYS ROCK !!!

    As suggested by JRV/Jalapeno42 after forcing the culture I got the way I wanted.

    My machine (Windows 7) is set to en-IN whereas Server 2008 R2 was set to en-US - which made the formatting to go crazy.

    PS C:\> Get-Culture

    LCID             Name             DisplayName
    ----             ----             -----------
    16393            en-IN            English (India)

    This command helped me a lot

    [datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy',[System.Globalization.CultureInfo]'en-US')

    Thanks again for your time.!

    Friday, May 29, 2015 3:04 PM