German date is not accepted in Powershell

Problem:

Systems or commands  working with english datetime do not accept german date formats. Converting them directly to Datetime ist not possible.
 “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
Example:

Solution:

Convert the date to an english datetime in case there are dots in it. German = 28.02.2017 , English 02/28/2017

$DateValue = "28.02.2017" # Play with this two values
$DateValue = "02/28/2017" # Play with this two values
 
 
if ($DateValue.contains("."))
            {
            $DateValue = [datetime]::parseexact($DateValue,"dd.MM.yyyy",$null)
            Write-host "de"
            }
Else
            {
            $DateValue = [datetime]$DateValue
            Write-host "en"
            }

 

This entry was posted in Information Technology and tagged . Bookmark the permalink.

Comments are closed.