using System.Globalization; public static DataTable GenerateDST(int startYear, int endYear) { DataTable dt = new DataTable(); //get the current timezone TimeZone oTimeZone = TimeZone.CurrentTimeZone; DaylightTime oDST; dt.Columns.Add("Year"); dt.Columns.Add("Start Date"); dt.Columns.Add("End Date"); for (int i = startYear; i <= endYear; i++) { oDST = oTimeZone.GetDaylightChanges(i); dt.Rows.Add(oDST.Start.Year, oDST.Start.ToShortDateString(), oDST.End.ToShortDateString()); } oTimeZone = null; oDST = null; return dt; }How to use
GenerateDST(2009, 2016)Result
PS:Don't forget to set your machine's timezone correctly.
How to check the given DateTime is DST or not?
Here is how in C# 2.0:
DateTime dt = DateTime.Parse("3/8/2009 01:00"); dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).Start) or dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).End)Give the following statements
DateTime dt = DateTime.Parse("3/8/2009 01:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).Start)); dt = DateTime.Parse("3/8/2009 02:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).Start)); dt = DateTime.Parse("3/8/2009 03:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).Start)); dt = DateTime.Parse("11/1/2009 01:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).End)); dt = DateTime.Parse("11/1/2009 02:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).End)); dt = DateTime.Parse("11/1/2009 03:00:00"); Console.WriteLine("{0} => {1}", dt.ToString(), dt.Equals(TimeZone.CurrentTimeZone.GetDaylightChanges(dt.Year).End));Result
First of all. Thanks very much for your useful post.
ReplyDeleteI just came across your blog and wanted to drop you a note telling you how impressed I was with the
information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for community.
There is a good C# resource site, Have alook
http://CSharpTalk.com
Thanks again
Rahul
The website you mentioned is helpful, and I have added it to my bookmark.
ReplyDeleteThanks!