Tip: Convert String <> Datetime
Datetime > String
1 2 3 4 5 |
# Solution 1 $datetimeToString = '{0:MM/dd/yy}' -f (Get-Date '07/15/2015') # Solution 2 $datetimeToString = (Get-Date '07/15/2015').ToShortDateString() |
String > Datetime
1 2 3 4 5 6 7 8 9 |
# Solution 1 $stringToDatetime1 = '07/15/2015' | Get-Date $stringToDatetime = '07-15-2015' | Get-Date # Solution 2 $stringToDatetime2 = [Datetime]::ParseExact('07/15/2015', 'MM/dd/yyyy', $null) # Solution 3 $stringToDatetime3 = [Datetime]'7/15/2015' |
MSDN:
DateTime.ParseExact Method