Reputation: 1441
I need to know how to convert a date time of GMT time zone format into other formats like Eastern, Pacific, Mountain and India time zone formats in C#, asp.net 2.0 and dot net framework 2.0.
Upvotes: 3
Views: 3939
Reputation: 15
Write a method like below, so can easily converted by calling it. Its for indian standard time
public DateTime IndianStandard(DateTime currentDate)
{
TimeZoneInfo mountain = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime utc = currentDate;
return TimeZoneInfo.ConvertTimeFromUtc(utc, mountain);
}
Upvotes: 0
Reputation: 1062915
Really, you want .NET 3.5 for this...
(I know you asked about .NET 2.0, but this answer may be heplful for somebody searching for this topic in the future)
TimeZoneInfo mountain = TimeZoneInfo.FindSystemTimeZoneById(
"US Mountain Standard Time");
DateTime utc = DateTime.UtcNow;
DateTime local = TimeZoneInfo.ConvertTimeFromUtc(utc, mountain);
Before then... pain. You can maintain your own list of offsets, but then you have to worry about DST.
Upvotes: 1
Reputation: 1500825
Using .NET 2.0 you're mostly stuffed, unfortunately. You'll need to use P/Invoke to create an instance of the TimeZone
class. From .NET 3.5 onwards it's a lot better - TimeZoneInfo
allows you to fetch non-local zones. I seem to remember that the latter has better support for historical time zone information too, instead of just a pair of rules for when DST changes.
You can get information using GetTimeZoneInformation but that's relatively ugly. There may well be some way of using P/Invoke just to make the conversion for you... although it's still likely to be hairy.
How firm is the requirement to use .NET 2.0? You'd save yourself a lot of hassle using .NET 3.5...
Upvotes: 2