Get Time Zone list in C#
Here, it is a sample code to get time zone list in C#
string listTimeZone="";
foreach (TimeZoneInfo tzi in TimeZoneInfo.GetSystemTimeZones())
listTimeZone += tzi.Id + " - " + tzi.DisplayName;
protected void Page_Load(object sender, EventArgs e)
{
var inputTime = Convert.ToDateTime("01/15/2019 04:30:00").ToUniversalTime();
TimeZone localZone = TimeZone.CurrentTimeZone;
lblMessage.Text = localZone.StandardName;
DateTime outPutTime = inputTime.ToTimeZoneTime(localZone.StandardName);
lblMessage.Text += "<br>" + inputTime.ToString();
lblMessage.Text += "<br>India = " + outPutTime.ToString();
lblMessage.Text += "<br>US Eastern Standard Time = " + inputTime.ToTimeZoneTime("US Eastern Standard Time").ToString();
}
public static class genericDate
{
public static DateTime ToTimeZoneTime(this DateTime time, string timeZoneId = "Pacific Standard Time")
{
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
return TimeZoneInfo.ConvertTimeFromUtc(time, tzi);
}
}
Enjoy !!!
:)
No comments:
Post a Comment