DateTime Globalization

T

Terry Olsen

I'm a bit confused on how to make my app work in all cultures, particularly
with parsing the date out of a string. I read dates from different web
sites. The formats I've run into include 'yyyy.mm.dd', 'dd/mm/yyyy',
'yyyy-mm-dd', 'dd-mmm-yyyy'.

I'm using the following code to parse the date, which has worked for a lot
of Regions but not all, such as Germany.

Dim CI As System.Globalization.CultureInfo
CI = System.Globalization.CultureInfo.GetCultureInfo("en-US")
Dim dt as String = <Date scraped from web page>
dt = Date.Parse(dt.Trim, CI.DateTimeFormat).ToShortDateString

When the Region is set to Germany, the Date.Parse line throws an exception.
The Message is "String was not recognized as a valid DateTime." In this
case, dt = "2007.03.03"

How can I make this code to produce a valid date string in any culture?

Thanks.
 
C

Cor Ligthert [MVP]

Terry,

On a webpage there is no globalization, I wish it was there. The only thing
you can do is put your date in a mask.

Germany has the same date time structure as Holland and most states of the
EU, that is not including the seperator, by instance in Holland those are
used in the way the head of the user is standing (as we say).

In the world there are 3 main structures when it is not about the seperator

dd mm yy (most used because it is almost complete the EU, Africa and India)
yy dd mm (secondly used because it is by instance the way in China)
mm dd yyyy(rarely used, only the USA and some Coca Cola cultures)

Cor
 
P

Patrice

Did you left intentionaly en-US ? I tried with de-de and it works. Also use
a constant string perhaps to make sure you don't have white spaces or non
printable characters n the string you are trying to parse...

The following works fine here (.NET 2.0) :

Dim CI As System.Globalization.CultureInfo
CI = System.Globalization.CultureInfo.GetCultureInfo("de-de")
Dim dt As String = "2007.03.03"
dt = Date.Parse(dt.Trim, CI.DateTimeFormat).ToShortDateString
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top