Convert To TimeZone

image_pdfimage_print
   
 

//Octavalent Extension Methods
//http://sdfasdf.codeplex.com/
//Library of extension methods for .Net create by Octavalent (www.octavalent.nl)

using System;
    public static class DateTimeExtensions
    {

        public static DateTime ConvertToTimeZone(this DateTime dateTime, string SystemTimeZoneId)
        {
            dateTime = dateTime.ToUniversalTime();
            dateTime = dateTime.Add(TimeZoneInfo.FindSystemTimeZoneById(SystemTimeZoneId).GetUtcOffset(dateTime));
            return dateTime;
        }
   }

   
     


The IsDaylightSavingTime and GetUtcOffset methods work as follows:

image_pdfimage_print
   
  

using System;
public class MainClass {
    public static void Main() {
        TimeZone zone = TimeZone.CurrentTimeZone;
        DateTime dt1 = new DateTime(2008, 1, 1);
        DateTime dt2 = new DateTime(2008, 6, 1);
        Console.WriteLine(zone.IsDaylightSavingTime(dt1));
        Console.WriteLine(zone.IsDaylightSavingTime(dt2));
        Console.WriteLine(zone.GetUtcOffset(dt1));
        Console.WriteLine(zone.GetUtcOffset(dt2));
    }
}