ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

CurrentLocalTimeForTimeZone

Returns the current local time for the specified time zone.

Source

public static DateTime CurrentLocalTimeForTimeZone(this TimeZoneInfo tzi)
{
    return System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, tzi.Id);
}

Example

static int Main()
{
    ReadOnlyCollection<TimeZoneInfo> arrTzi = TimeZoneInfo.GetSystemTimeZones();
    foreach(TimeZoneInfo tzinfo in arrTzi)
    {
        DateTime dt = tzinfo.CurrentLocalTimeForTimeZone();
        Console.Write(tzinfo.Id);
        Console.Write(" ");
        Console.Write(tzinfo.DisplayName);
        Console.Write(" : ");
        Console.WriteLine(dt.ToString("F"));
    }
    return 0;
}

Author: Greg Lyon

Submitted on: 14 mei 2009

Language: C#

Type: System.TimeZoneInfo

Views: 2072