简体   繁体   English

如何从年,月,月中的星期几和星期几中获取日期? C#

[英]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

Presidents day this year WAS on the 15th of February, so that was correct. 今年的总统日是2月15日,所以这是正确的。 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. 我的劳动节是这样工作的:在第一部分中,我不得不假设您想将Microsoft的DateTime(星期日= 0)转换为您的Sunday = 1枚举。

    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 在主函数中,您要迭代直到实际到达星期几,而不仅仅是减少星期几(一旦到达星期日,循环将始终停止,因为(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. 我用几年后的日期对它进行了测试,它似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM