static DateTime todaysDate = new DateTime(2016,03,31,11,33,11);
public static void Main()
DateTime startDate = new DateTime();
DateTime aDueDate = new DateTime();
startDate = DateTime.Now;
startDate = new DateTime(2016,04,01,08,26,44);
Console.WriteLine( "Start Date : " + startDate.ToString("dd-MMM-yyyy HH:mm:ss") + " Todays Date: " + todaysDate.ToString("dd-MMM-yyyy HH:mm:ss") );
int result = DateTime.Compare(startDate, todaysDate );
relationship = "is earlier than";
relationship = "is the same time as";
relationship = "is later than";
Console.WriteLine(System.Environment.NewLine);
Schedule sType = new Schedule();
aDueDate = CalculateDueDateBySchedule(startDate, sType);
Console.WriteLine( sType.TypeName + " " + sType.HourInterval+ " Due Date: " + aDueDate.ToString("dd-MMM-yyyy HH:mm:ss") );
aDueDate = CalculateDueDateBySchedule(startDate, sType);
Console.WriteLine( sType.TypeName + " " + sType.HourInterval+ " Due Date: " + aDueDate.ToString("dd-MMM-yyyy HH:mm:ss") );
Console.WriteLine(System.Environment.NewLine);
sType.DayName = "Friday";
Console.WriteLine(sType.DayName + " "+ GetDayOftheWeekInt(sType.DayName ) );
aDueDate = CalculateDueDateBySchedule(startDate, sType);
Console.WriteLine( sType.TypeName + " " + sType.DayName + " Week Interval: " + sType.WeekInterval+ " First Due Date: " + aDueDate.ToString("dd-MMM-yyyy HH:mm:ss") );
Console.WriteLine(System.Environment.NewLine);
sType.TypeName = "month";
sType.MonthDate = "first";
aDueDate = CalculateDueDateBySchedule(startDate, sType);
Console.WriteLine( sType.TypeName + " " + sType.MonthDate + " " + sType.MonthInterval+ " Due Date: " + aDueDate.ToString("dd-MMM-yyyy HH:mm:ss") );
sType.TypeName = "month";
sType.MonthDate = "last";
aDueDate = CalculateDueDateBySchedule(startDate, sType);
Console.WriteLine( sType.TypeName + " " + sType.MonthDate + " " + sType.MonthInterval+ " Due Date: " + aDueDate.ToString("dd-MMM-yyyy HH:mm:ss") );
public static int GetDayOftheWeekInt(string daySelected)
switch(daySelected.ToLower())
dayInt = (int) DayOfWeek.Monday;
dayInt = (int) DayOfWeek.Tuesday;
dayInt = (int) DayOfWeek.Wednesday;
dayInt = (int) DayOfWeek.Thursday;
dayInt = (int) DayOfWeek.Friday;
dayInt = (int) DayOfWeek.Saturday;
dayInt = (int) DayOfWeek.Sunday;
public static DateTime CalculateDueDateBySchedule(DateTime aDate, Schedule sType)
DateTime aDueDate = new DateTime();
aDueDate = CalculateDueDateHour(aDate, sType.HourInterval);
aDueDate = ClosestWeekDay(aDate, sType.DayName, sType.WeekInterval);
aDueDate = CalculateDueDateMonth(aDate, sType.MonthDate, sType.MonthInterval);
public static DateTime CalculateDueDateWeek(DateTime startDate, string daySelected, int weekInterval){
DateTime nextWeekDate = new DateTime();
int weekFraction = weekInterval*7;
int dayInt = GetDayOftheWeekInt(daySelected);
int dayIntofStartDate = GetDayOftheWeekInt(startDate.DayOfWeek.ToString());
Console.WriteLine(dayInt + " "+ dayIntofStartDate);
nextWeekDate = startDate.AddDays(( dayInt - dayIntofStartDate) + weekFraction) ;
public static DateTime ClosestWeekDay(DateTime startDate, string daySelected, int weekInterval)
int daySelectedInt = GetDayOftheWeekInt(daySelected);
int dayIntStartDate = GetDayOftheWeekInt(startDate.DayOfWeek.ToString());
if(dayIntStartDate > daySelectedInt)
return startDate.AddDays(7*weekInterval - ( dayIntStartDate - daySelectedInt));
if(dayIntStartDate == daySelectedInt)
if(startDate > todaysDate)
return startDate.AddDays( daySelectedInt - dayIntStartDate);
return startDate.AddDays(7*weekInterval - ( dayIntStartDate - daySelectedInt));
return startDate.AddDays( daySelectedInt - dayIntStartDate);
public static DateTime CalculateDueDateMonth(DateTime aDate, string MonthDate, int monthInterval)
DateTime dueDate = new DateTime();
int year = aDate.AddMonths(monthInterval).Year;
int month = aDate.AddMonths(monthInterval).Month;
if(MonthDate.ToLower() == "first")
year = aDate.AddMonths(monthInterval).Year;
month = aDate.AddMonths(monthInterval).Month;
dueDate = new DateTime(year, month, 1, hours, mins, secs);
dueDate = new DateTime(year, month - 1, DateTime.DaysInMonth(System.DateTime.Now.Year, month -1), hours, mins, secs);
if(MonthDate.ToLower() == "last")
dueDate = new DateTime(year, month, DateTime.DaysInMonth(System.DateTime.Now.Year, month), hours, mins, secs);;
dueDate = new DateTime(year, month + 1, DateTime.DaysInMonth(System.DateTime.Now.Year, month+1), hours, mins, secs);
public static DateTime CalculateDueDateHour(DateTime aDate, int hourInterval){
return aDate.AddHours(hourInterval);
public string TypeName {get; set;}
public int HourInterval{get;set;}
public string DayName {get; set;}
public int WeekInterval{get;set;}
public string MonthDate {get; set;}
public int MonthInterval {get; set;}