Hi all, wondered if you can help. Is there anyway I can convert a custom defined date as dd/MM/yyyy to MM/dd/yyyy at all please?
So I have got the variable:
$date = “23/12/2021”
How can I convert $date to format MM/dd/yyyy? Is there any way I can do this? I’ve looked all over and can’t find any answers to this.
Thanks,
Dan
Olaf
December 20, 2021, 12:05am
2
Dan,
Welcome to the forum.
Really? Where have you you searched and whatfor?
It depends a little bit on your locale
setting but something like this should get you started.
$date = '23/12/2021'
Get-Date $date -Format 'MM\/dd\/yyyy'
BTW: When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance
1 Like
Thank you for the reply but sadly did not resolve what I am trying to do. I should have put some background on my post.
But I am trying to get the $date to work with DateTime. As I want to convert the 23/12/2021 to 12/23/2021 so I can do, for example:
Set-ADAccountExpiration -Identity "test_user" -DateTime $converteddate
However at the moment it is coming up with…
Cannot bind parameter. Cannot convert value “23/12/2021” to type “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
Olaf
December 20, 2021, 2:11am
4
Definitely.
$date = '23/12/2021'
$converteddate = Get-Date $date
Another option could be this … what would be a kind of language independend:
$date = '23/12/2021'
$SplittedDate = $date -split '/'
$converteddate = Get-Date -Day $SplittedDate[0] -Month $SplittedDate[1] -Year $SplittedDate[2]
You’re a star mate. Thank you! It works