How to convert string to local date time?

I am trying to convert a string of this format:

MM/dd/yyyy HH:mm 

Logging in is from the US database, that is, i.e.: 09/20/2010 14:30

I know that my string is always in the USA, but when I show it, I need to translate this to local time so that the string is converted to:

 09/20/2010 19:30 (for UK for instance) 

I tried a few things, but nothing seems to give me the correct solution when I run the US machine against the UK or the Ge machine I tried:

 CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm", CultureInfo.CurrentCulture); CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm", new CultureInfo("en-US")); 

They all work locally (US machine), but they do not convert the time into local time on a European machine.

Thank you, Tony

+4
source share
6 answers

Try this - it converts local time (input in US format) to GMT and then prints in GB / DE format.

 var zones = TimeZoneInfo.GetSystemTimeZones(); // retrieve timezone info string value = "09/20/2010 14:30"; DateTime CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm", new CultureInfo("en-US")); DateTime FinalDttm = TimeZoneInfo.ConvertTime(CompletedDttm, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"), TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time")); string output = FinalDttm.ToString(new CultureInfo("en-GB")); FinalDttm = TimeZoneInfo.ConvertTime(CompletedDttm, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")); output = FinalDttm.ToString(new CultureInfo("de-DE")); 

The output, in turn:

09/20/2010 19:30:00

09/20/2010 20:30:00

+3
source

UPDATE: You need to know the time zone of the data (not just "US"), as well as the interpreter, if you want to reliably convert this to nothing else. You not only look at the clock, but also the DST, which depends on the location (not all locales observe it). East is -4 or -5 depending on the time of year. And if the date is old enough, you are faced with the problem that the "summer" dates have recently been changed.

Your best course is ALWAYS to store timestamps in UTC. Other than that, you can just guess about the bias.


You must work with UTC (a new, slightly different version of GMT) if you want to convert to other time zones.

 DateTime dt = new DateTime(DateTime.Parse('2010-10-06 19:40').Ticks, DateTimeKind.Local); dt.AddHours(5); dt.ToLocalTime(); 

You can also use TimeZoneInfo , which will also have DST information.

+4
source

Unless you specify otherwise, parsing will assume that you want to parse a string in the current time zone. US culture means only the expected format of the string and has nothing to do with the time zone (for example, in the US it can be EST, or it can be PST).

Your line does not contain time zone information, so naturally you will get your value in all local time zones. You can:

+3
source

I think this is a display problem, but you need more information to be sure. Try displaying dates in yyyy-MM-dd format in both cases to see if the problem is parsing or displaying. You can create a custom-format information object if you know exactly what you want to accept or display:

  public static DateTimeFormatInfo GetISOFormatInfo() { DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); dtFormat.DateSeparator = "-"; dtFormat.TimeSeparator = ":"; dtFormat.ShortDatePattern = "yyyy-MM-dd"; dtFormat.ShortTimePattern = "HH:mm:ss"; return dtFormat; } 
0
source

Using a date without time zone information, you will not be able to find out the UK time / time in Canada, etc., since you do not know who (which part of the world) entered at that time. Since you specifically said that the time in the USA, you can add time differences for different parts of the world to display local time.

0
source

You can use string.Split. first with a delimiter '/' on the whole line. You will get β€œ09” β€œ20” and β€œ2010 14:30”, then apply the split 2 more times with β€œand”:

-2
source

All Articles