#powershell Get-Date v.s. [datetime] Format and System Locale II

Irrespective of the Windows system language and locale, a non-US datetime format text string imported from a file into PowerShell may still need to be formated with Get-Date or cast through [datetime] prior to use.

However, date/time operations may fail silently if the conversion or re-formatting did not succeed. The trick is to use the ParseExact static method in Get-Date or [datetime] in a manner such as:

$dt = “31.12.2017 23:59:59”
$dt1 = [datetime]::ParseExact($dt, ‘dd.MM.yyyy HH:mm:ss’,$null)

 

Leave a Reply