[英]Get saturday date value from a selected week using a month calendar
I am kinda stuck about this problem. 我有点担心这个问题。 How can I get the month calendar saturday values when i selected a specific date. 当我选择特定日期时,如何获得月历日历值。
For example: i selected February 14 on the month calendar. 例如:我在月历上选择了2月14日。 After selecting it there will be a prompt which contains Saturday "February 19, 2011" or i selected February 24, The prompt will display "February 26 2011". 选择它后会有一个提示,其中包含星期六“2011年2月19日”或我选择的2月24日,提示将显示“2011年2月26日”。
// This function will return the next saturday for a datetime
DateTime NextSaturday(DateTime now)
{
while (now.DayOfWeek != DayOfWeek.Saturday)
now = now.AddDays(1);
return now;
}
UPDATE UPDATE
After almost 2 years I want to change this answer. 差不多2年后,我想改变这个答案。
These days I would never create a "utility function" for a class
. 这些天我永远不会为一个class
创建一个“实用功能”。 I now always "extend" the class. 我现在总是“延伸”课程。 The signature should now be DateTime.Next(DayOfWeek)
. 签名现在应该是DateTime.Next(DayOfWeek)
。 See http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx for more details on extensions. 有关扩展的更多详细信息,请参见http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx 。
Also the logic is wrong. 逻辑也错了。 If now
is a Saturday then it would always return the current date. 如果now
是星期六,那么它将始终返回当前日期。 I think most callers would expect it return now
+ 7 days. 我想大多数来电者都会期望它now
返回+ 7天。 So the first change is: 所以第一个变化是:
DateTime NextSaturday(DateTime now)
{
do {
now = now.AddDays(1);
} while (now.DayOfWeek != DayOfWeek.Saturday)
return now;
}
Then change the function to work with any day of the week: 然后将功能更改为与一周中的任何一天一起使用:
DateTime Next(DateTime now, DayOfWeek nextDay)
{
do {
now = now.AddDays(1);
} while (now.DayOfWeek != nextDay)
return now;
}
Now "extend" the DateTime class to support Next(DayOfWeek) 现在“扩展”DateTime类以支持Next(DayOfWeek)
namespace DateTime.Extensions
{
public static class DateTimeExtensions
{
public static DateTime Next(this DateTime now, DayOfWeek nextDay)
{
do {
now = now.AddDays(1);
} while (now.DayOfWeek != nextDay)
return now;
}
}
}
DateTime add = DateTime.Now; //From popup box
int add = (((int)selected.DayOfWeek) + 1;
if(add != 7) {
selected = selected.AddDays(add);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.