Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Created November 21, 2023 10:10
Show Gist options
  • Save ststeiger/20f8964b8d3cbc595b14ea57f995c530 to your computer and use it in GitHub Desktop.
Save ststeiger/20f8964b8d3cbc595b14ea57f995c530 to your computer and use it in GitHub Desktop.
Compute Black Friday date
namespace ExportDatabase
{
class AmHoliday
{
public static System.DateTime Thanksgiving(int year)
{
if (year < 1942)
throw new System.ArgumentException("Year must be 1942 or later.", nameof(year));
System.DateTime dt = new System.DateTime(year, 11, 1, 0, 0, 0, System.DateTimeKind.Local);
int diff = System.DayOfWeek.Thursday - dt.DayOfWeek;
if (diff < 0)
diff += 7;
diff += 22;
System.DateTime thanksGiving = new System.DateTime(year, 11, diff, 0, 0, 0, System.DateTimeKind.Local);
return thanksGiving;
} // End Function Thanksgiving
private static System.DateTime Thanksgiving2(int year)
{
if (year < 1942)
throw new System.ArgumentException("Year must be 1942 or later.", nameof(year));
System.DateTime dt = new System.DateTime(year, 11, 1, 0, 0, 0, System.DateTimeKind.Local);
int[] thanks = new int[] { 26, 25, 24, 23, 22, 28, 27 };
System.DateTime thanksGiving = new System.DateTime(year, 11, thanks[(int)dt.DayOfWeek], 0, 0, 0, System.DateTimeKind.Local);
return thanksGiving;
} // End Function Thanksgiving2
public static System.DateTime BlackFriday(int year)
{
if (year < 1942)
throw new System.ArgumentException("Year must be 1942 or later.", nameof(year));
System.DateTime dt = Thanksgiving(year).AddDays(1);
return dt;
} // End Function BlackFriday
internal static void Test()
{
System.DateTime tg = Thanksgiving(2024);
System.DateTime tg2 = Thanksgiving2(tg.Year);
System.DateTime bf = tg.AddDays(1);
System.DateTime dt = new System.DateTime(tg.Year, 11, 1, 0, 0, 0, System.DateTimeKind.Local);
string[] weekDays = new string[] {
"Sunday" // 0
,"Monday" // 1
,"Tuesday" // 2
,"Wednesday" // 3
,"Thursday" // 4
,"Friday" // 5
,"Saturday" // 6
};
int thanks = -1;
switch (dt.DayOfWeek)
{
case System.DayOfWeek.Thursday: // 22 + (thursday - weekday)
thanks = 22;
break;
case System.DayOfWeek.Wednesday:// 22 + (thursday - weekday)
thanks = 23;
break;
case System.DayOfWeek.Tuesday:// 22 + (thursday - weekday)
thanks = 24;
break;
case System.DayOfWeek.Monday: // 22 + (thursday - weekday)
thanks = 25;
break;
case System.DayOfWeek.Sunday: // 22 + (thursday - weekday)
thanks = 26;
break;
case System.DayOfWeek.Saturday: // 22 + 7 - (thursday - weekday)
thanks = 27;
break;
case System.DayOfWeek.Friday: // 22 + 7 - (thursday - weekday)
thanks = 28;
break;
} // End switch
int diff = System.DayOfWeek.Thursday - dt.DayOfWeek;
if (diff < 0)
diff += 7;
System.Console.WriteLine("November 1st {0} is a {1}.", dt.Year, weekDays[(int)dt.DayOfWeek]);
System.Console.WriteLine("Thanksgiving {0} is on November {1}.", dt.Year, 22 + diff);
System.Console.WriteLine("Thanksgiving {0} is on November {1}.", dt.Year, thanks);
System.Console.WriteLine("Thanksgiving {0} is on {1}.", tg.Year, tg.ToString("dddd' 'dd' 'MMMM' 'yyyy"));
System.Console.WriteLine("Thanksgiving (2) {0} is on {1}.", tg2.Year, tg2.ToString("dddd' 'dd' 'MMMM' 'yyyy"));
System.Console.WriteLine("Black Friday {0} is on {1}.", bf.Year, bf.ToString("dddd' 'dd' 'MMMM' 'yyyy"));
} // End Sub Test
} // End Class AmHoliday
} // End Namespace ExportDatabase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment