【问题标题】:Convert DateTime from UTC to EST in .Net 3.0在 .Net 3.0 中将 DateTime 从 UTC 转换为 EST
【发布时间】:2011-08-31 22:27:41
【问题描述】:

我正在使用 .Net 3.0,我想将 DateTime 值从 UTC 转换为 EST/EDT(也需要加入夏令时)。

我知道在TimeZoneInfo class 的帮助下,使用 .Net 3.5 可以直接实现这一目标。我不想使用Timezone.CurrentTimeZone,因为无论本地计算机的时区如何,我都希望将此值转换为 EST/EDT。不幸的是,切换到 3.5 不是一种选择。在互联网上搜索显示了使用系统注册表和其他东西的选项。

有没有更简单的方法来做到这一点? 谁能引导我朝着正确的方向前进并让我知道实现这一目标的选项?

【问题讨论】:

    标签: c#-3.0 timezone


    【解决方案1】:

    以下函数确定特定的 DateTime 对象是否代表东部时区的夏令时。

    public static bool IsEasternDaylightTime(DateTime dt){
            // Find out whether it's Daylight Saving Time
            dt=dt.AddHours(-5); // Convert to Eastern Standard Time
            if(dt.Year<=2006){
                // 2006 and earlier
                if(dt.Month<=3 || dt.Month>=11){
                    // Standard Time
                    return false;
                } else if(dt.Month>=5 && dt.Month<=9){
                    // Daylight Time
                    return true;
                } else if(dt.Month==4){
                    // find the first Sunday of April
                    int firstSunday=1;
                    while(new DateTime(dt.Year,dt.Month,firstSunday).DayOfWeek!= DayOfWeek.Sunday){
                        firstSunday++;
                    }
                    if(dt.Day<firstSunday)
                        return false;
                    else if(dt.Day>firstSunday)
                        return true;
                    else {
                        // DST begins at 2AM
                        if(dt.Hour<2)
                            return false; // Standard Time
                        else if(dt.Hour>=3)
                            return true; // Daylight Time
                        else
                            return false; // Ambiguous Time
                    }
                } else {
                    // find the last Sunday of October
                    int lastSunday=1;
                    for(int i=1;i<=31;i++){
                        if(new DateTime(dt.Year,dt.Month,i).DayOfWeek== DayOfWeek.Sunday){
                            lastSunday=i;
                        }
                    }
                    if(dt.Day<lastSunday)
                        return true;
                    else if(dt.Day>lastSunday)
                        return false;
                    else {
                        // DST ends at 2AM
                        if(dt.Hour<1)
                            return true; // Daylight Time
                        else if(dt.Hour>=2)
                            return false; // Standard Time
                        else
                            return false; // Standard Time
                    }
                }
            } else {
                // 2007 and later
                if(dt.Month<=2 || dt.Month>=12){
                    // Standard Time
                    return false;
                } else if(dt.Month>=4 && dt.Month<=10){
                    // Daylight Time
                    return true;
                } else if(dt.Month==3){
                    // find the second Sunday of March
                    int sundays=0;
                    int lastSunday=1;
                    for(int i=1;i<=31;i++){
                        if(new DateTime(dt.Year,dt.Month,i).DayOfWeek== DayOfWeek.Sunday){
                            lastSunday=i;
                            sundays++;
                            if(sundays==2)break;
                        }
                    }
                    if(dt.Day<lastSunday)
                        return false;
                    else if(dt.Day>lastSunday)
                        return true;
                    else {
                        // DST begins at 2AM
                        if(dt.Hour<2)
                            return false; // Standard Time
                        else if(dt.Hour>=3)
                            return true; // Daylight Time
                        else
                            return false; // Ambiguous Time
                    }
                } else {
                    // find the first Sunday of November
                    int firstSunday=1;
                    while(new DateTime(dt.Year,dt.Month,firstSunday).DayOfWeek!= DayOfWeek.Sunday){
                        firstSunday++;
                    }
                    if(dt.Day<firstSunday)
                        return true;
                    else if(dt.Day>firstSunday)
                        return false;
                    else {
                        // DST ends at 2AM
                        if(dt.Hour<1)
                            return true; // Daylight Time
                        else if(dt.Hour>=2)
                            return false; // Standard Time
                        else
                            return false; // Standard Time
                    }
                }
            }
        }
    

    使用此函数,您可以相应地转换给定的 DateTime。示例:

     // dateTime is assumed to be in UTC, not local time
     bool dst=IsEasternDaylightTime(dateTime);
     if(dst)
         dateTime=dateTime.AddHours(-4);
     else
         dateTime=dateTime.AddHours(-5);       
    

    【讨论】:

    • 非常感谢您的回复!这有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2021-10-27
    • 2021-11-15
    • 2010-10-17
    • 2020-04-03
    • 2011-06-07
    • 1970-01-01
    相关资源
    最近更新 更多