在.Net 3.0中将日期时间从UTC转换为EST

发布于 2024-12-02 15:43:23 字数 274 浏览 1 评论 0原文

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

据我所知,在 TimeZoneInfo 类的帮助下,使用 .Net 3.5 可以直接实现这一点。我不想使用 Timezone.CurrentTimeZone ,因为我希望将此值转换为 EST/EDT,而不管本地计算机的时区如何。不幸的是,切换到 3.5 不是一个选择。在互联网上搜索发现了使用系统注册表之类的选项。

有更简单的方法吗? 有人可以引导我走向正确的方向,并让我知道实现这一目标的选择吗?

I am using .Net 3.0 and I want to convert a DateTime value from UTC to EST/EDT (need Daylight savings incorporated too).

I understand that this would be straight-forward to achieve using .Net 3.5, with the help of TimeZoneInfo class. I do not want to use Timezone.CurrentTimeZone because I want this value converted to EST/EDT irrespective of the timezone of the local computer. Unfortunately switching to 3.5 is not an option. Search on the internet revealed options of using system registry and stuff.

Is there an easier way of doing this?
Could anyone please lead me in the right direction and let me know the options to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2024-12-09 15:43:23

以下函数确定特定的 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 is assumed to be in UTC, not local time
 bool dst=IsEasternDaylightTime(dateTime);
 if(dst)
     dateTime=dateTime.AddHours(-4);
 else
     dateTime=dateTime.AddHours(-5);       

The following function determines whether a particular DateTime object represents Daylight Saving Time in the Eastern Time Zone.

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
                }
            }
        }
    }

With this function, you can convert the given DateTime accordingly. Example:

 // 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);       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文