Need help in formatting Date and Time!

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.

May 29th, 2015 9:31am

This work probably work - 

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

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 9:45am

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?

May 29th, 2015 9:53am

[datetime]::ToDay.AddDays(75).ToString('MM/dd/yyyy')
Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 10:22am

PowerShell -f operator can format a date as a string in any format you want.

May 29th, 2015 10:33am

No Luck JRV :-( , still getting dashes.

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 10:35am

Odd.  I get the slashes as expected.

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

May 29th, 2015 10:42am

No Luck JRV :-( , still getting dashes.

I don't think so. What are you running? XP?
Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 10:45am

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

(Get-Culture).DateTimeFormat

  • Marked as answer by Vasu Deva 15 hours 56 minutes ago
May 29th, 2015 10:47am

/ 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.

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 10:50am

We can force culture:

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

May 29th, 2015 10:54am

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

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 10:59am

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.!

May 29th, 2015 11:06am

/ 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
Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 2:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics