9

I have a problem with time
My server is in the USA and I'm in Denmark (Europa) and I would like to have my site show the time in my local time. How can I do that?

I try this

Datetime localtime = DateTimeOffset.Now.ToOffset(new TimeSpan(1,0,0)).DateTime;

and it works, but it will only work when I'm in GMT+1 / UTC+1 and not when I'm in GMT+2 / UTC+2. Is there another way of doing this - a simpler way of doing it?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Nesizer
  • 2,484
  • 6
  • 20
  • 24

4 Answers4

11

The only way you should do it is as follows:

string zoneId = "Central European Standard Time";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow,tzi);
Console.WriteLine("Time is " + result + " in Denmark");

Using the TimeZoneInfo class is the only reliable way in .Net to convert to/from different timezones AND get proper DST conversions.

TimeZoneInfo.ConvertTimeToUtc(dtLocal,tzi) is the reverse converting from a local time to a utc time.


For the TimeZone Id strings, you can run the bit of code here...

foreach( var tz in TimeZoneInfo.GetSystemTimeZones() )
{
    Console.WriteLine(tz.DisplayName + " is Id=','" + tz.Id + "'");
}
Robert Paulson
  • 16,639
  • 4
  • 31
  • 51
  • 1
    It should be noted that TimeZoneInfo correctly takes DST differences into account for future and past times as well. Don't be tempted to do the conversions yourself using BaseUtcOffset or GetUtcOffset(). You can also check for invalid and ambiguous times using TimeZoneInfo. – Robert Paulson Nov 26 '08 at 02:43
  • If timezone is not known? for everybody in different countries how can we do it? – Jeyhun Rahimov Mar 25 '13 at 07:18
  • @JhoonBey allow people to select their country, or use a service that determines the country from their IP address. e.g. http://stackoverflow.com/q/13921563/14033 – Robert Paulson Sep 04 '13 at 10:29
0

I did the conversion to be displayed in a gridview using a template field.

<asp:TemplateField HeaderText="Last Activity">
                <ItemTemplate>
                    <asp:Label ID="LastActivityLBL" runat="server" Text='<%# Convert.ToDateTime(Eval("LastActivityDate")).ToLocalTime() %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Last Login">
                <ItemTemplate>
                    <asp:Label ID="LastLoginLBL" runat="server" Text='<%# Convert.ToDateTime(Eval("LastLoginDate")).ToLocalTime() %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
cal5barton
  • 1,527
  • 1
  • 12
  • 24
0

You can get the time off the server and do this.

DateTime myTimeGMT = ServerTime.ToUniversalTime();

This do this:

DateTime myTimeLocal = myTimeGMT.ToLocalTime();

The only restriction here is the computer you are on must be set to the time zone you are converting to.

In my experience, .NET has trouble converting between timezones when neither the 'From' time or the 'To' time are the local time zone.

I hope this helps.

HitLikeAHammer
  • 2,549
  • 2
  • 35
  • 51
0
Datetime localtime = DateTimeOffset.Now.ToOffset(new TimeSpan(1,0,0)).DateTime;

You can change your TimeSpan like--

Datetime localtime = DateTimeOffset.Now.ToOffset(new TimeSpan(3,0,0)).DateTime;

according to the time zone.

Shree Krishna
  • 7,883
  • 6
  • 34
  • 66