Reputation: 351
I have a variable, $date, containing 24 June 2012 00:00:00
.
How do I convert this to 24/06/2012
?
Upvotes: 7
Views: 36087
Reputation: 1
I tried reading a file with dates formatted day-month-year The answer above did not work for me, I found a different solution on how to parse my dates and check which one is newer than the current date. This is my adapted code.
$currdateobj = Get-Date
$STARTDATE = "12-05-2017" // specific non-default date format
[DateTime]$startdateobj = [DateTime]::ParseExact($STARTDATE,"dd-MM-yyyy",[System.Globalization.CultureInfo]::InvariantCulture)
if ($startdateobj -lt $currdateobj)
{
// ....
}
Upvotes: 0
Reputation: 126712
Use the Get-Date
cmdlet together with the Format parameter:
PS> $date = '24 June 2012 00:00:00'
PS> Get-Date $date -Format 'dd/MM/yyyy'
24/06/2012
Upvotes: 11