none
How to get first and last date of provided week RRS feed

  • Question

  •  

    Hi All,

           I am using VS03 + c# +Winforms.

    How can i calculate start and last date of given week number.

    Inshort if user enter Week number = 10 at that time i want to get first date and last date of that particular week.

     

    The code in c# is appriciated.

    Thanks in advance.

    Thursday, July 31, 2008 11:31 AM

Answers

  • This may not be the best way but it seems to work...
    Use the DateTime class, something like this:

    Code Snippet

    // set date to the first day of the year
    DateTime dt = new DateTime(2008, 1, 1);
    int weekNumber = 34;
    // mult by 7 to get the day number of the year
    int days = (weekNumber-1) * 7;
    // get the date of that day
    DateTime dt1 = dt.AddDays(days);
    // check what day of week it is
    DayOfWeek dow = dt1.DayOfWeek;
    // to get the first day of that week - subtract the value of the DayOfWeek enum from the date
    DateTime startDateOfWeek = dt1.AddDays(-(int)dow);
    MessageBox.Show(startDateOfWeek.ToShortDateString());



    hth,
    Lior.
    Thursday, July 31, 2008 12:33 PM

All replies

  • This may not be the best way but it seems to work...
    Use the DateTime class, something like this:

    Code Snippet

    // set date to the first day of the year
    DateTime dt = new DateTime(2008, 1, 1);
    int weekNumber = 34;
    // mult by 7 to get the day number of the year
    int days = (weekNumber-1) * 7;
    // get the date of that day
    DateTime dt1 = dt.AddDays(days);
    // check what day of week it is
    DayOfWeek dow = dt1.DayOfWeek;
    // to get the first day of that week - subtract the value of the DayOfWeek enum from the date
    DateTime startDateOfWeek = dt1.AddDays(-(int)dow);
    MessageBox.Show(startDateOfWeek.ToShortDateString());



    hth,
    Lior.
    Thursday, July 31, 2008 12:33 PM
  • Hi,

            Thanks for the reply.I have gone through the code.It is working perfectly for my application.

     

     

    Cheers!

     

    Thursday, July 31, 2008 1:07 PM
  • It Works for me. Thank You
    Thursday, January 16, 2020 6:07 PM