Convert local time to UTC in .NET framework 3.0

本秂侑毒 提交于 2019-12-13 14:30:21

问题


I develop an app. in c#, I should translate local time to UTC, the local time is in time zone that who that use in my app. enters. I must use in .NET framework 3.0, so can't use the TimeZoneInfo object.

Does anyone has an idia how can I do it?

Should use in TimeZone Object?

Thanks

Maybe I can't do it?


回答1:


Now I see the problem. Use the following method instead: TimeZone.ToUniversalTime




回答2:


every time I save time in a database I ALWAYS save in UTC time

myEntity.CreateDate = DateTime.UtcNow;

Now, in 3.0 or less

use this file

http://pastebin.com/w20NRkuP

it contains a helper so you can list all Timezones and have their values. Add it to your db or use on-the-fly.

Fill up a dropdown and ask user to choose it's own TimeZone, then, just add the Minutes to your saved UTC Date.

for example:

<asp:DropDownList ID="myDropDown" runat="server" />

then

myDropDown.DataSource = Helper.ListAllTimeZones();
myDropDown.DataValueField = "UtcOffsetMinutes";
myDropDown.DataTextField = "DisplayName";

myDropDown.DataBind();

when saving user preferences:

User.OffSet = (int)myDropDown.SelectedValue;

hope it helps


for example, sweetie.com does this:

just request the timezone to the user and save all in UTC time, then just add/subtract the timezone.



来源:https://stackoverflow.com/questions/4668921/convert-local-time-to-utc-in-net-framework-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!