Hi i am converting time to ESt using offset using below function
public static DateTimeOffset ConvertToTheaterTimeZone2(string TheaterTimeZone, DateTimeOffset Date)
{
TimeZoneInfo tzi2 = TimeZoneInfo.FindSystemTimeZoneById(TheaterTimeZone);
TimeSpan offset2 = tzi2.GetUtcOffset(DateTime.Now);
Date = Date.UtcDateTime;
Date = Date.AddMinutes(offset2.TotalMinutes);
return Date;
}
and then use difference hour calculation using following way but not getting accurate hours plz suggest any other way
var hour = (item.Checkindate - ConvertToTheaterTimeZone2("Eastern Standard Time", DateTime.Now)).TotalHours;
The time zone ID "Eastern Standard Time" doesn't actually mean "Eastern Standard Time". It really means "Eastern Time"... From TimeZoneInfo.Id
:
The value of the Id property is usually, but not always, identical to that of the StandardName property. The identifier of the Coordinated Universal Time zone is UTC.
So basically, this will give you UTC-5 during winter, and UTC-4 during summer. That's usually what you want... but you don't want to perform the conversion in that way. It won't compile (for various reasons) and is overly complciated. You should just use:
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
return TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, zone);
You may also want to consider using my Noda Time library, which makes a lot of this more fool-proof, as well as allowing you to use the more standard IANA time zone database.
See more on this question at Stackoverflow