简体   繁体   English

如何获得每周的日期,并且还与53周的年份一起工作? C#

[英]How do I get date from week, working also with 53-week years? c#

I have made a function to cound the weeks in a year, and that works fine. 我做了一个函数,可以计算一年中的几周,效果很好。 The problem is that I need a method to get the mondaydate of the week. 问题是我需要一种获取星期几的方法。 This is a swedish calendar. 这是瑞典的日历。

The code below works well for years that have 52 weeks, but some years(like 2009) has 53 weeks. 下面的代码对52周的年份有效,但某些年份(如2009年)为53周。 Then I got a date from januari as the mondaydate(cant be right). 然后我从januari得到了一个日期作为mondaydate(不能正确)。 So please help me to get it to work for all years. 因此,请帮助我使它能够正常工作多年。

What I probably could do is check if the year has 53 weeks and then do some checks but I'd like it to go smooth without special checks. 我可能可以做的是检查这一年是否有53周,然后再进行一些检查,但是我希望它能在没有特殊检查的情况下顺利进行。

Here's what I've come up with: 这是我想出的:

    public static DateTime GetDateFromWeek(int year, int week)
    {
        //First day of the year
        DateTime d = new DateTime(year, 1, 1);
        GregorianCalendar calendar = new GregorianCalendar(GregorianCalendarTypes.MiddleEastFrench);
        d = calendar.AddWeeks(d, week);
        d = d.AddDays(1 - (double)d.DayOfWeek);
        return d;
    }

I think your base problem is the assumption that DateTime d = new DateTime(year, 1, 1); 我认为您的基本问题是假设DateTime d = new DateTime(year, 1, 1); is in the first week of the year, but it could belong to week 52/53 of the previous year. 是在一年的第一周,但它可能属于上一年的第52/53周。

You will find a solution here . 您将在此处找到解决方案。

This should do it: 应该这样做:

public static DateTime GetDateFromWeek(int year, int week)
{
    GregorianCalendar calendar = new GregorianCalendar(GregorianCalendarTypes.MiddleEastFrench);
    DateTime d = new DateTime(year, 12, 31);
    int weeksInYear = calendar.GetWeekOfYear(d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    int weeksToSubtract = weeksInYear - week;
    d = calendar.AddWeeks(d, -weeksToSubtract);
    d = d.AddDays(1 - (int)d.DayOfWeek);
    return d;
}

You might want to have a look at the following question, I think it is what you are asking: 您可能想看看以下问题,我想这就是您要问的问题:

Get date of first Monday of the week? 获取一周中第一个星期一的日期?

if (cmb_mode_of_service.SelectedItem.ToString() == "Weekly Service")
            {
                int year = 0;
                if (cmb_term_of_service.SelectedItem.ToString() == "One Year")
                {
                    year = 1;
                }

                if (cmb_term_of_service.SelectedItem.ToString() == "Two Year")
                {
                    year = 2;
                }

                if (cmb_term_of_service.SelectedItem.ToString() == "three year")
                {
                    year = 3;
                }

                DateTime currentdate = Convert.ToDateTime(service_start_date.Text);
                DateTime Enddate = currentdate.AddYears(+year);


                char c1 = 'A';
                int c2 = 1;
                for (var dt1 = currentdate; dt1 <= Enddate; dt1 = dt1.AddDays(7))
                {
                    DataRow dr = dt.NewRow();
                    dr["SN"] = c2++;
                    dr["serviceid"] = "S4-" + c1++;
                    dr["servicedate"] = dt1.ToString();
                    dr["servicestatus"] = "Pending";
                    dr["serviceexcutive"] = "Not Alowed";
                    dt.Rows.Add(dr);

                }
                dataGridView1.DataSource = dt;

            }

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

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