Reputation: 4175
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?
Upvotes: 3
Views: 12446
Reputation: 75133
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
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.
Upvotes: 0
Reputation: 10604
Now I see the problem. Use the following method instead: TimeZone.ToUniversalTime
Upvotes: 6