C# 从UTC转换为时区时出现奇怪错误

C# 从UTC转换为时区时出现奇怪错误,c#,timezone,C#,Timezone,我无法将UTC时间转换为相应的本地时间 我拥有的是字符串形式的UTC时间和位置时区(如“东部标准时间”) 我的代码: private static DateTime? LocalTimeConvert(string locationTimeZone, DateTime? dateTimeUtc) { var dateTimeUnspec = DateTime.SpecifyKind(dateTimeUtc.Value, DateTimeKind.Unspecified); Da

我无法将UTC时间转换为相应的本地时间

我拥有的是字符串形式的UTC时间和位置时区(如“东部标准时间”)

我的代码:

private static DateTime? LocalTimeConvert(string locationTimeZone, DateTime? dateTimeUtc)
{

    var dateTimeUnspec = DateTime.SpecifyKind(dateTimeUtc.Value, DateTimeKind.Unspecified);
    DateTime utcDateTime = TimeZoneInfo.ConvertTime(dateTimeUnspec, TimeZoneInfo.FindSystemTimeZoneById(locationTimeZone));

    return utcDateTime;
}

public static T LocalTime<T>(T value, string locationTimeZone)
{
    if (value.GetType().IsGenericType && value.GetType().GetGenericTypeDefinition() == typeof(List<>))
    {
        IList collection = (IList)value;

        foreach (var element in collection)
        {
            VariableConvertion(element, locationTimeZone);
        }
    }
    else
    {
        VariableConvertion(value, locationTimeZone);
    }

    return value;
}


private static T VariableConvertion<T>(T value, string locationTimeZone)
{
    PropertyInfo[] props = value.GetType().GetProperties();

    foreach (var property in props)
    {
        if (property.PropertyType == typeof(DateTime?) || property.PropertyType == typeof(DateTime))
        {
            if (property.GetValue(value) != null)
            {
                var localTime = LocalTimeConvert(locationTimeZone, DateTime.Parse(property.GetValue(value).ToString()));
                property.SetValue(value, localTime);
            }
        }
    }

    return value;
}
中欧标准时间-不正确 我还想知道为什么我在美国服务器上的时间不同,在波兰服务器上的时间不同。我在angular中使用pipe表示日期:“short”:“UTC”,所以我应该在不进行本地转换的情况下从数据库中获取准确的日期,对吗?或者C#中的DateTime也在将日期转换为服务器日期?

请参阅
TimeZoneInfo.ConvertTime(DateTime,TimeZoneInfo)
方法的

备注部分中,它表示如果源
日期时间
种类
属性是
日期时间种类.未指定
,则假定它是本地的。由于您在代码中将种类显式设置为
Unspecified
,因此您不是从UTC转换到指定时区,而是从本地时间转换到指定时区

在此上下文中,“本地时间”是指执行代码的计算机时区设置的本地时间。因此,如果您在波兰的服务器设置为波兰时区,那么转换为波兰时间将是不可操作的

如果要将UTC转换为特定时区,则源值的
种类
应为
DateTimeKind.UTC
,或者应使用
ConvertTimeFromUtc
而不是
ConvertTime
(区别在于,
未指定的
种类假定为
Utc
而不是
本地


顺便说一句,
DateTime.Parse(property.GetValue(value.ToString)()有一个潜在的bug
。永远不要仅仅为了将对象解析回对象而从对象中创建字符串。相反,请强制转换对象以将其取消装箱到所需类型。tostring/parse方法通常比较慢,并且经常引入与当前区域性的日期格式相关的错误。

因为您使用的是更改时间的ConvertTime。所有计算机以UTC为单位存储时间,然后在输入字符串或输出字符串时,使用机器上的时区设置自动转换。当使用ConvertTime时,您正在更改时间。波兰时间为+2,东部标准时间为-4。因此,如果是UTC午夜,则为东部标准时间晚上8:00和波兰时间凌晨2:00。如果您同时转到这两台机器并要求显示UTC时间,您将获得两台机器上都是午夜。谢谢!我唯一需要更改的是ConvertTimeFromUtc。当我搜索问题的解决方案时,我不知道该方法。我还将该错误更改为:DateTime localDateTime=Convert.ToDateTime(property.GetValue(value));var localTime=LocalTimeConvert(locationTimeZone,localDateTime);property.SetValue(value,localTime);谢谢:)
    System.TimeZoneInfo.FindSystemTimeZoneById returned {(UTC-05:00) Stany Zjednoczone i Kanada (czas wschodni)}    System.TimeZoneInfo
    locationTimeZone    "Eastern Standard Time" string
    dateTimeUtc {19.03.2021 15:43:07}   System.DateTime?
    dateTimeUnspec  {19.03.2021 15:43:07}   System.DateTime
    utcDateTime {19.03.2021 10:43:07}   System.DateTime
    System.TimeZoneInfo.FindSystemTimeZoneById returned {(UTC+01:00) Sarajewo, Skopie, Warszawa, Zagrzeb}   System.TimeZoneInfo
    locationTimeZone    "Central European Standard Time"    string
    dateTimeUtc {22.03.2021 12:58:09}   System.DateTime?
    dateTimeUnspec  {22.03.2021 12:58:09}   System.DateTime
    utcDateTime {22.03.2021 12:58:09}   System.DateTime