3

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?

RRR
  • 3,937
  • 13
  • 51
  • 75
  • 4
    See - http://stackoverflow.com/questions/2548235/convert-utc-datetime-to-another-time-zone or http://stackoverflow.com/questions/3439468/utc-to-users-local-time - and reverse the algorithm – ChrisF Jan 12 '11 at 12:45
  • I can't use in TimeZoneInfo object, because it is no in framework 3.0 – RRR Jan 12 '11 at 13:02

2 Answers2

6

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

Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68
  • I know, but how can I know in my app what the UTC offset of the TimeZone that the user enter? and how can I takes into account the daylight saving time rule? – RRR Jan 12 '11 at 12:58
  • This is done for you automatically (assuming your app is local, not web). Check examples on MSDN. – Artem Koshelev Jan 12 '11 at 13:05
  • sorry, I don't understand, which is object in c# done it automatically? where is the example in MSDN? – RRR Jan 12 '11 at 13:14
  • DateTime.ToUniversalTime is the link pointing to MSDN article. There are usage examples at the bottom of the page. – Artem Koshelev Jan 12 '11 at 13:20
  • it is good to convert local time in the time zone that the computer is located to UTC, but in my app I get from the user Time zone - that it is no my time zone, and date in this time zone, and I should translate from this time zone to UTC. the DateTime.ToUniversalTime help only to convert dateTime in computer time zone to UTC. – RRR Jan 12 '11 at 13:36
  • Check update. Details depends on how you get the time zone offset from user. – Artem Koshelev Jan 12 '11 at 13:50
  • sould I ask from the user the offset? or I have a option to calculate by myself in my app. as the TimeZoneInfo? – RRR Jan 12 '11 at 13:59
  • I can't say anything until I know what's the application type and how it interacts with the user and user's operating system. – Artem Koshelev Jan 12 '11 at 14:07
0

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:

alt text

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

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • but I don't have a TimeZoneInfo object in FrameWork 3.0 – RRR Jan 12 '11 at 13:51
  • dumb me :( to get a list of TimeZones in 3.0: http://staceyw1.wordpress.com/2006/08/31/convert-local-time-to-timezone/ big and stupip, but it's the only way, or get it once and create your own object with this info. – balexandre Jan 12 '11 at 15:13
  • added a class so it would be easier to use. – balexandre Jan 12 '11 at 15:27