TimeZoneInfo class holds the information of TimeZone properties i.e., TimeZone Display Name, TimeZone ID, All the available TimeZones and the Local TimeZone.
The below code snippet explains the real time implementation to get the TimeZone Corresponding Time.
private DateTime GetBaseDateTime(string sourceTimeZoneName) { TimeZoneInfo sourceTimeZone = null; //loop through all the TimeZones to get source TimeZone object foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones()) { if (tz.DisplayName.ToUpper() == sourceTimeZoneName.ToUpper()) { sourceTimeZone = tz; break; } } //TimeZoneInfo.Local gives the local timezone DateTime sourceTime = DateTime.MinValue; if (null != sourceTimeZone) { //get the source datetime based on the source timezone sourceTime = TimeZoneInfo.ConvertTime(DateTime.Now, sourceTimeZone); } return sourceTime; }
No comments:
Post a Comment