Week Ending...

Last post 01-24-2008 6:20 AM by MNF. 9 replies.

Sort Posts:

  • Week Ending...

    04-06-2005, 10:56 AM
    • Member
      156 point Member
    • ewallig
    • Member since 04-06-2005, 2:50 PM
    • Posts 117

    Hi,

    Building an ASPx timesheet for work - how can I figure out Saturday's date for the current week? Basically, I want to display a line on the page that reads "Week Ending: (_Saturday's Date_)" and then use that value throughout the rest of the timesheet.

    I think that I did something like this a while back in classic ASP but I can't find my reference to it.

    Thanks...

  • Re: Week Ending...

    04-06-2005, 12:12 PM
    • Star
      7,879 point Star
    • jamesqua
    • Member since 08-03-2004, 6:55 PM
    • Columbus, OH
    • Posts 1,362

    You can get the current day and then use a Select Case statement to add a number of days to it.  For example,

    Select Case VariableofCurrentDay

    Case Monday

    'add five days

    Case Tuesday

    'add four days

    End Select

    Josh Harrison
    Community Coder
  • Re: Week Ending...

    04-06-2005, 12:19 PM
    • Star
      13,992 point Star
    • stevenbey
    • Member since 10-28-2002, 8:56 AM
    • United Kingdom
    • Posts 2,808

    Try the following:

          int numberOfDaysToSaturday = DayOfWeek.Saturday - DateTime.Now.DayOfWeek;

          DateTime saturday = DateTime.Now.AddDays(numberOfDaysToSaturday);

    Steven Bey

    Recursion: see Recursion
  • Re: Week Ending...

    04-06-2005, 2:28 PM
    • Member
      156 point Member
    • ewallig
    • Member since 04-06-2005, 2:50 PM
    • Posts 117

    To stevenby -

    Ok, that works but I noticed that it returns a 6 for Saturday, isn't it supposed to be a 7?

     

    Thanks Tongue Tied [:S]

  • Re: Week Ending...

    04-06-2005, 4:17 PM
    • Contributor
      2,037 point Contributor
    • BasicDatePicker
    • Member since 10-22-2004, 1:32 PM
    • Edmonton, Alberta, Canada
    • Posts 406

    Hi ewallig,

    Give the following Method a try. Example:

    /// <summary>
    ///
    Gets the next occurence of future day.
    /// </summary>
    ///
    <param name="value">DateTime value to start with.</param>
    ///
    <param name="dayOfWeek">Next Day of week to find.</param>
    public DateTime GetNextOccurenceOfDay(DateTime value, DayOfWeek dayOfWeek)
    {
          DateTime tempDate =
    value.AddDays(1);
          while(tempDate.DayOfWeek != dayOfWeek)
          {
          tempDate = tempDate.AddDays(1);
          }
          return tempDate;
    }

    Pass your date the next DayOfWeek you want to find and it will return you the date as a DateTime object. Can be used to find the next occurence of any DayOfWeek, not just Saturday.

    Hope this helps.

    Thanks,
    Geoffrey McGill - Product Manager
    Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
  • Re: Week Ending...

    04-11-2005, 4:49 AM
    • Star
      13,992 point Star
    • stevenbey
    • Member since 10-28-2002, 8:56 AM
    • United Kingdom
    • Posts 2,808

     ewallig wrote:
    Ok, that works but I noticed that it returns a 6 for Saturday, isn't it supposed to be a 7?

    Microsoft regard Sunday as the first day of the week and as we use 0 indexing this means that Saturday has a value of 6.

    Steven Bey

    Recursion: see Recursion
  • Re: Week Ending...

    04-11-2005, 8:09 AM
    • Member
      156 point Member
    • ewallig
    • Member since 04-06-2005, 2:50 PM
    • Posts 117

    Hi BasicDatePicker

    Thanks for the example - I'll give it a try Big Smile [:D]

  • Re: Week Ending...

    04-12-2005, 8:34 AM
    • Star
      13,992 point Star
    • stevenbey
    • Member since 10-28-2002, 8:56 AM
    • United Kingdom
    • Posts 2,808

     BasicDatePicker wrote:
    public DateTime GetNextOccurenceOfDay(DateTime value, DayOfWeek dayOfWeek)
    {
          DateTime tempDate =
    value.AddDays(1);
          while(tempDate.DayOfWeek != dayOfWeek)
          {
          tempDate = tempDate.AddDays(1);
          }
          return tempDate;
    }

    I like the idea but think that using a loop is inefficient. I suggest the following:

       int daysToAdd = dayOfWeek - value.DayOfWeek;

       if(daysToAdd < 1) {

          daysToAdd += 7;

       } 

       return value.AddDays(daysToAdd);

    Steven Bey

    Recursion: see Recursion
  • Re: Week Ending...

    04-12-2005, 3:49 PM
    • Contributor
      2,037 point Contributor
    • BasicDatePicker
    • Member since 10-22-2004, 1:32 PM
    • Edmonton, Alberta, Canada
    • Posts 406

    Hi stevenbey,

    Nice work. Much better.

    Here's the final code for all posterity in Visual Basic and C#.

    [Visual Basic]

    Public Function GetNextOccurenceOfDay(ByVal value As DateTime, ByVal dayOfWeek As DayOfWeek) As DateTime
          
    Dim daysToAdd As Integer = dayOfWeek - value.DayOfWeek
          If daysToAdd < 1 Then
                daysToAdd += 7
          End If
          Return value.AddDays(daysToAdd)
    End Function

    [C#]

    /// <summary>
    ///
    Gets the next occurence of future day.
    /// </summary>
    ///
    <param name="value">DateTime value to start with.</param>
    ///
    <param name="dayOfWeek">Next Day of week to find.</param>
    public DateTime GetNextOccurenceOfDay(DateTime value, DayOfWeek dayOfWeek)
       {
          int daysToAdd = dayOfWeek - value.DayOfWeek;
          if(daysToAdd < 1) 
          {
                daysToAdd += 7;
          } 
          return value.AddDays(daysToAdd);
    }

    Thanks,
    Geoffrey McGill - Product Manager
    Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
  • Re: Week Ending...

    01-24-2008, 6:20 AM
    • Member
      259 point Member
    • MNF
    • Member since 07-24-2005, 8:15 PM
    • Posts 72

    Thanks for the code. I've included the function into my DateTimeHelper class and added extra methods like NextMonday and ClosestMonday(if passing parameter is Monday, the passing value will be return.

Page 1 of 1 (10 items)