简体   繁体   English

如何将字符串转换为本地日期时间?

[英]How to convert string to local Date Time?

I am trying to convert a string of this format: 我试图转换这种格式的字符串:

MM/dd/yyyy HH:mm

The input is from a US database, so, ie: 09/20/2010 14:30 输入来自美国数据库,即: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 谢谢托尼

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. 更新:如果要将数据可靠地转换为其他任何内容,您必须知道数据的时区 (不仅仅是“US”)以及解释机器。 You're not only looking at hours offset, but DST also which varies by location (not all locales abide by it). 您不仅要查看小时偏移量,而且DST也会根据位置而变化(并非所有区域都遵守它)。 Eastern is either -4 or -5 depending on the time of year. 东部是-4或-5,取决于一年中的时间。 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. 您最好的方法是始终以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. 如果您想要转换到其他时区,您应该使用UTC时间(新的,略有不同的GMT版本)。

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. 您也可以使用TimeZoneInfo ,它也会有DST信息。

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). 美国文化只是意味着字符串的预期格式 ,与时区无关(例如,在美国它可能是EST,也可能是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 之后更改时区

Try this - it converts local time (input in US format) to GMT and then prints in GB/DE format. 试试这个 - 它将当地时间(以美国格式输入)转换为GMT,然后以GB / DE格式打印。

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 19:30:00

20.09.2010 20:30:00 20.09.2010 20:30:00

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. 尝试在两种情况下以yyyy-MM-dd格式显示日期,以检查问题是解析还是显示。 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;
    }

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. 使用没有TimeZone信息的日期,您将无法知道英国时间/加拿大时间等...因为您不知道谁(世界的哪个部分)在那个时间插入。 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. 由于您特别说时间是美国时间,您可以添加世界不同地区的时差来显示当地时间。

You could use string.Split. 你可以使用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 ':' 您将获得“09”“20”和“2010 14:30”,然后再使用''和':'分割2次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM