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. Then I got a date from januari as the mondaydate(cant be right). 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.

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;
    }

Answers


I think your base problem is the assumption that 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.

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;

            }

Need Your Help

How to get a list of all available Com Ports in C# with asp.net 4

c# serial-port

How can I list all of the available com ports to my computer?