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.
<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,
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
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,
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
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.
ewallig
Member
170 Points
129 Posts
Week Ending...
Apr 06, 2005 02:56 PM|LINK
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...
jamesqua
Star
8249 Points
1417 Posts
Re: Week Ending...
Apr 06, 2005 04:12 PM|LINK
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
stevenbey
All-Star
16438 Points
3369 Posts
Re: Week Ending...
Apr 06, 2005 04:19 PM|LINK
Try the following:
int numberOfDaysToSaturday = DayOfWeek.Saturday - DateTime.Now.DayOfWeek;
DateTime saturday = DateTime.Now.AddDays(numberOfDaysToSaturday);
http://stevenbey.com
Recursion: see Recursion
ewallig
Member
170 Points
129 Posts
Re: Week Ending...
Apr 06, 2005 06:28 PM|LINK
To stevenby -
Ok, that works but I noticed that it returns a 6 for Saturday, isn't it supposed to be a 7?
Thanks [:S]
BasicDatePicker
Contributor
2037 Points
406 Posts
Re: Week Ending...
Apr 06, 2005 08:17 PM|LINK
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.
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
stevenbey
All-Star
16438 Points
3369 Posts
Re: Week Ending...
Apr 11, 2005 08:49 AM|LINK
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.
http://stevenbey.com
Recursion: see Recursion
ewallig
Member
170 Points
129 Posts
Re: Week Ending...
Apr 11, 2005 12:09 PM|LINK
Hi BasicDatePicker
Thanks for the example - I'll give it a try [:D]
stevenbey
All-Star
16438 Points
3369 Posts
Re: Week Ending...
Apr 12, 2005 12:34 PM|LINK
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);
http://stevenbey.com
Recursion: see Recursion
BasicDatePicker
Contributor
2037 Points
406 Posts
Re: Week Ending...
Apr 12, 2005 07:49 PM|LINK
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 DateTimeDim 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);
}
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
MNF
Member
261 Points
73 Posts
Re: Week Ending...
Jan 24, 2008 10:20 AM|LINK
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.
My Blog: http://geekswithblogs.net/mnf/