Converting Culture-Specific Information

by Dec 8, 2017

Let’s assume you received data like numbers or date as text. Whenever information is reduced to text, you are challenged with culture-specific formats: decimal point as well as date and time formats may differ between cultures.

Here is a simple approach how you can parse data provided you know the culture where it came from:

# number in German format
$info = '1,2'

# convert to real value
$culture = New-Object -TypeName CultureInfo("de")
$number = [Double]::Parse($info,$culture)

Each target type has a Parse() method, so if you receive a date and/or time information, you can convert it just as easily. This example takes date and time formatted to French standards, and returns it as a true DateTime object:

# date and time in French format:
$info = '01/11/2017 16:28:45'

# convert to real value
$culture = New-Object -TypeName CultureInfo("fr")
$date = [DateTime]::Parse($info,$culture)
$date

Twitter This Tip! ReTweet this Tip!