GThree
GThree

Reputation: 3510

How to get a date from year, month, week of month and Day of week ? C#

I have month, year, day of week and week of month, based on that I want to get a date. How can I do that? Following is my code but I am not getting correct result.

What am I missing here?

Code:

internal DateTime? GetDate(int year) // year = 2016
{
    int month;
    DateTime? date;

    switch (Type)
    {
        case "CALCULATED":
            month = FixedMonth; // month = 9
            var firstDayOfWeek = new DateTime(year, month, 1); // firstDayOfWeek = 9/1/2016 12:00:00 AM
            while (RestaurantSchedule.GetOneBasedDayOfWeek(firstDayOfWeek) <= DayOfWeek) // DayOfWeek = Monday
            {
                firstDayOfWeek = firstDayOfWeek.AddDays(1);
            }
            date = firstDayOfWeek.AddDays((WeekOfMonth - 1) * 7); // WeekOfMonth = 1, returns date = 9/1/2016 12:00:00 AM
            if (date.Value.Month > month)
            {
                return date.Value.AddDays(-7);
            }
            return date;
        default:
            return new DateTime?();
    }
}

public static OneBasedDayOfWeek GetOneBasedDayOfWeek(DateTime date) // date = 9/1/2016 12:00:00 AM
{
    // returns Thursday
    return (OneBasedDayOfWeek)Enum.Parse(typeof(OneBasedDayOfWeek), date.DayOfWeek.ToString());
}

// OneBasedDayOfWeek uses following enum        
public enum DayOfWeek
{
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7 
}

Database entry:

HolidayDefinitionId   Type         FixedMonth  FixedDay    DayOfWeek   WeekOfMonth Adjustment  HolidayName     Enabled
--------------------  ------------ ----------- ----------- ----------- ----------- ----------- --------------- -------
LABOR                 CALCULATED   9           0           2           1           0           Labor Day       1
PRESIDENTS            CALCULATED   2           0           2           3           0           President's Day 1

Actual Results:

Presidents Day = 02/15/2016
Labor Day = 09/01/2016

Expected Results:

Presidents Day = 02/16/2016
Labor Day = 09/05/2016

Upvotes: 0

Views: 459

Answers (1)

Kolichikov
Kolichikov

Reputation: 3020

Presidents day this year WAS on the 15th of February, so that was correct. I got labour day to work like this: For the first part I had to assume that you wanted to translate Microsofts DateTime (sunday = 0) to your Sunday = 1 enum.

    public static DayOfWeek GetOneBasedDayOfWeek(DateTime date) // date = 9/1/2016 12:00:00 AM
    {
        // returns Thursday
        return (DayOfWeek)((int)date.DayOfWeek + 1);
    }

And in your main function, you want to iterate until you have actually reached your day of the week, not just while it's less (once you reach Sunday, your loop will always stop, since (int)Sunday < (int)AnyOtherDay

while (GetOneBasedDayOfWeek(firstDayOfWeek) != DayOfWeek)

I tested this with the dates a few years down the road and it seemed to work.

Upvotes: 1

Related Questions