Wednesday, November 15, 2017

C# code sample calculating US Holidays List for a given year programmatically

C# code sample to calculate US holidays list for any given year programmatically. All these holidays occur on a specific week in a month of year.

  •  MLK Day 
  •  President's Day 
  •  Memorial Day 
  •  Labor Day 
  •  Columbus Day 
  •  Thanksgiving Day

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Linq;

namespace holidayhelper
{
    class Program
    {
        static void Main(string[] args)
        {
            int year = 2017;
            Console.WriteLine("{0,-20}:{1}","MLK Day",HolidayHelper.MlkDay(year));
            Console.WriteLine("{0,-20}:{1}","Presidents Day",HolidayHelper.PresidentDay(year));
            Console.WriteLine("{0,-20}:{1}","Memorial Day",HolidayHelper.MemorialDay(year));
            Console.WriteLine("{0,-20}:{1}","Labor Day",HolidayHelper.LaborDay(year));
            Console.WriteLine("{0,-20}:{1}","Columbus Day",HolidayHelper.ColumbusDay(year));
            Console.WriteLine("{0,-20}:{1}","Thanks Giving Day",HolidayHelper.ThanksgivingDay(year));
        }
    }
    public static class HolidayHelper{
        //MLK Day - Jan
        public static string MlkDay(int year)
        {
            return NthDayOfMonth(year, 1, DayOfWeek.Monday, 3);
        }
        // President's Day - Feb
        public static string PresidentDay(int year)
        {
            return NthDayOfMonth(year, 2, DayOfWeek.Monday, 3);
        }
        //Memorial Day - May last week
        public static string MemorialDay(int year)
        {
            return LastMondayOfMay(year);
        }
        //Labor Day - Sept First week
        public static string LaborDay(int year)
        {
            return NthDayOfMonth(year, 9, DayOfWeek.Monday, 1);
        }
        ///Columbus Day - october second week
        public static string ColumbusDay(int year)
        {
            return NthDayOfMonth(year, 10, DayOfWeek.Monday, 2);
        }
        ///ThanksGiving Day - nov Fourth week
        public static string ThanksgivingDay(int year)
        {
            return NthDayOfMonth(year, 11, DayOfWeek.Thursday, 4);
        }
        
         /// <summary>
        /// returns the nth day of given month (like 1st monday of month, 3rd tuesday of month or 4th monday of month)
        /// </summary>
        /// <param name="year">input year</param>
        /// <param name="month">input month</param>
        /// <param name="dow">Day of week</param>
        /// <param name="n">nth day</param>
        /// <returns>Datetime for the nthday</returns>
        private static string NthDayOfMonth(int year, int month, DayOfWeek dow, int n)
        {
            var days = DateTime.DaysInMonth(year, month);
            var nthday = (from day in Enumerable.Range(1, days)
                let dt = new DateTime(year, month, day)
                where dt.DayOfWeek == dow && (day - 1) / 7 == (n - 1)
                select dt).FirstOrDefault();
            return nthday.ToShortDateString();
        }

         /// <summary>
        /// returns the last monday date for memorial day for the given year. 
        /// </summary>
        /// <param name="year">input year</param>
        /// <returns>Memorial day date</returns>
        private static string LastMondayOfMay(int year)
        {
            var dt = new DateTime(year, 5, 31);
            while (dt.DayOfWeek != DayOfWeek.Monday)
            {
                dt = dt.AddDays(-1);
            }
            return dt.ToShortDateString();
        }
    }
}