如何在ASP.NET时区工作? [英] How to work with time zones in ASP.NET?

查看:212
本文介绍了如何在ASP.NET时区工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个在线提醒系统项目(ASP.NET 2.0(C#)/ SQL Server 2005中)

I am working on an "online reminder system" project (ASP.NET 2.0 (C#) / SQL Server 2005)

由于这是一个提醒服务,将邮件发送给用户一个特定的日期。但问题是,用户不能从一个特定的国家,他们来自世界各地,并从不同的时区。现在,当我注册,我以同样的方式询问用户的时区窗口询问我们的时区在安装时间。

As this is a reminder service which will send the mail to users on a particular dates. But the problem is users are not from a specific countries, they are from all over the world and from different time zones. Now When I am registering I am asking for users time zone in the same way as windows asks our time zone at the time of installation.

但我没有得到的,如果(5.30)选择的用户或某事时区,然后如何在我的asp.net应用程序处理这个时区。如何根据时区工作。

But I am not getting the if the user selected (+5.30) or something timezone then how to handle this time zone in my asp.net application. How to work according to timezone.

和请建议是否有更好的方法来处理这​​个应用程序时区?

And please suggest if there is any better way to handle timezones in this application ??

感谢

推荐答案

第一件事是确保你的数据是在哪个时区,我建议确保您存储的日期时间,存储在UTC时间(用在 DateTime.ToUniversalTime()来弄个)。

First thing is to make sure which time zone your data is in. I would recommend making sure that any DateTime that you store, is stored in UTC time (use the DateTime.ToUniversalTime() to get hold of it).

当你来存储用户的提醒,则需要当前UTC时间,添加或删除用户的时差,并转换成新的时间回UTC;这是你想要在数据库存储的东西。

When you are to store a reminder for a user, you will need the current UTC time, add or remove the user's time zone difference, and convert that new time back to UTC; this is what you want to store in the DB.

然后,当你想检查提醒的发送,你只需要在数据库中查找提醒,现在送出去,按照UTC时间;实际上得到有一个时间戳,是之前 DateTime.Now.ToUniversalTime所有提醒()

Then, when you want to check for reminders to send, you simply need to look in the database for reminders to send out now, according to UTC time; essentially get all reminders that have a time stamp that is before DateTime.Now.ToUniversalTime().

更新一些实现细节:
你可以从 TimeZoneInfo.GetSystemTimeZones()法时区的列表;可以使用那些以显示用户的时区列表。如果您选择的时区存储编号属性,你可以从它创建的TimeZoneInfo类的实例,并计算UTC时间定本地日期/时间值:

Update with some implementation specifics: You can get a list of time zones from the TimeZoneInfo.GetSystemTimeZones() method; you can use those to show a list of time zones for the user. If you store the Id property from the selected time zone, you can create a TimeZoneInfo class instance from it, and calculate the UTC time for a given local date/time value:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("<the time zone id>");
// May 7, 08:04:00
DateTime userDateTime = new DateTime(2009, 5, 7, 8, 4, 0);
DateTime utcDateTime = userDateTime.Subtract(tzi.BaseUtcOffset);

这篇关于如何在ASP.NET时区工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆