how to convert server time to local time

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 + "'");
}

Method 2

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.

Method 3

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>

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x