Reputation: 21452
i have hosted a site on a server that have different time zone than my local country,
how can I convert the server time to my local time with taking into consideration the Daylight Saving
Upvotes: 0
Views: 2212
Reputation: 3279
DateTimeOffset newTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("AppTimeZone"));
Upvotes: 3
Reputation: 52798
Have you looked at the TimeZoneInfo
class. You should be able to convert to and from local to server time through there whilst maintaining DST.
There is also an Open Source time zone implementation by Jon Skeet et. al call Noda-Time.
Upvotes: 1
Reputation: 150108
You can use the TimeZone class to convert from the server's local time to UTC, then from UTC to your (or the particular website user's) time zone.
Upvotes: 1
Reputation: 247680
to blatantly take answers from other SO questions:
In .NET 3.5, there is TimeZoneInfo, which provides a lot of functionality in this area; 2.0SP1 has DateTimeOffset, but this is much more limited.
See the following SO questions:
Get DateTime For Another Time Zone Regardless of Local Time Zone
Convert UTC DateTime to another Time Zone
Or just search SO for a lot of answers including some that take into consideration Daylight Savings.
Upvotes: 2