How to convert string to local Date Time?

2019-04-09 10:55发布

I am trying to convert a string of this format:

MM/dd/yyyy HH:mm

The input is from a US database, so, i.e.: 09/20/2010 14:30

I know that my string is always US time but when I display it, I need to translate that into the local time, so that string should be turned into:

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 on a US machine vs a UK or 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 don't convert the time to local time on a European machine.

Thanks Tony

6条回答
SAY GOODBYE
2楼-- · 2019-04-09 11:11

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"));

Output is, in turn:

20/09/2010 19:30:00

20.09.2010 20:30:00

查看更多
家丑人穷心不美
3楼-- · 2019-04-09 11:13

Using a Date without TimeZone information, you will not be able to know the UK time / Canada time etc... since you do not know who (which part of the world) instered that time. Since you specifically said that the time is US time, you can add the time difference for the different parts of the world to display the local time.

查看更多
【Aperson】
4楼-- · 2019-04-09 11:16

UPDATE: You have to know the timezone of the data (not just that it is "US") as well as the interpreting machine if you want to reliably convert it to anything else. You're not only looking at hours offset, but DST also which varies by location (not all locales abide by it). Eastern is either -4 or -5 depending on the time of year. And if the date is old enough you run into the issue that "summer" dates were changed recently.

Your best course is to ALWAYS store timestamps in UTC. Aside from that, you can just make guesses about the offset.


You should be working with UTC times (the new, slightly different, version of GMT) if you want to be converting to other time zones.

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

You could also make use of TimeZoneInfo which will have DST information also.

查看更多
贼婆χ
5楼-- · 2019-04-09 11:19

You could use string.Split. first with the '/' separator on the whole string. You will get "09" "20" and "2010 14:30" then apply the split 2 more times with ' ' and ':'

查看更多
Emotional °昔
6楼-- · 2019-04-09 11:20

I think it's a display problem, but need more info to be sure. Try displaying the dates in yyyy-MM-dd format in both cases to check if the problem is on parse or display. You can create a custom format info 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;
    }
查看更多
混吃等死
7楼-- · 2019-04-09 11:34

Unless you specify otherwise, the parse will assume you mean to parse the string into your current timezone. US culture just means the expected format of the string, and has nothing to do with the timezone (for example, in the US it could be EST or it could be PST).

Your string contains no timezone information, so naturally you're going to get your value in whatever the local timezone is. You can either:

  1. Add the timezone info
  2. Change the timezone afterwards
查看更多
登录 后发表回答