DateTime Problem

  • Thread starter Thread starter Werner Wopienka
  • Start date Start date
W

Werner Wopienka

HI NG!

I need to convert a de-DE date and time into an en-US and back again.
Bringing the German date and time in the en-US works perfectly, but back i
get a Format exception.

I tried:
Dim cultureus As New CultureInfo("en-US")
Dim culturede As New CultureInfo("de-DE")
Dim datestringus As DateTime
Dim timestringus As DateTime
Dim timestringde As DateTime
Dim datestringde As DateTime
datestringus = DateTime.Parse(DateTime.Now.ToShortDateString,
cultureus.DateTimeFormat, DateTimeStyles.AdjustToUniversal)
timestringus = DateTime.Parse(DateTime.Now.ToShortTimeString,
cultureus.DateTimeFormat, DateTimeStyles.AdjustToUniversal)
datestringde = DateTime.Parse(datestringus.ToString,
culturede.DateTimeFormat, DateTimeStyles.AdjustToUniversal)
timestringde = DateTime.Parse(timestringus.ToString,
culturede.DateTimeFormat, DateTimeStyles.AdjustToUniversal)

Thx 4 any help
Werner
 
Werner,

There's no "de-DE" DateTime or "en-US" DateTime.
DateTime stores time as a number of 100 ns intervals passed since 1/1/0001
12:00am (00:00) and is culture independent.

Culture is only relevant if you're converting string to DateTime or
DateTime to string.

For example:

DateTime dt = DateTime.Now; // Culture independent

Console.WriteLine ("en-US: {0}", dt.ToString (new CultureInfo("en-US")));
// Print it in US format
Console.WriteLine ("de-DE: {0}", dt.ToString (new CultureInfo("de-DE")));
// Print it in German (or should I say Europe?) format

dt = DateTime.Parse ("21.04.2004 12:10:01", new CultureInfo("de-DE")); //
Parse string in German format

Console.WriteLine ("en-US: {0}", dt.ToString (new CultureInfo("en-US")));
// Print it in US format.

Sample output:

en-US: 4/21/2004 12:18:55 PM
de-DE: 21.04.2004 12:18:55
en-US: 4/21/2004 12:10:01 PM

So, as soon as you have your DataTime value (say, parsed from a string with
culture correctly set), you can print it out in any culture format you want.

Or, are you having problems with time zones? You can use
DateTime.ToUniversalTime() and DateTime.ToLocalTime() to solve it.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Back
Top