简体   繁体   English

使用月历来获取所选周的星期六日期值

[英]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.

相关问题 从Winforms月份日历中选择月份? - Get selected month from Winforms month calendar? 从月份日历控件中删除选定的日期 - Remove selected date from a month calendar control C#Web表单-寻找从日历中选择日期之后的月份的第一个星期一的日期 - C# web form - Looking to get the date of the first monday of the month after the selected date from the calendar 如何从我们当前日期获取整个工作周(从周一到周六的一天) - How to get the whole working week(Day's from Monday to Saturday), from our current date 从选定的月份获取星期 - Get week from selected monthcalendar-date 如何从年,月,月中的星期几和星期几中获取日期? C# - How to get a date from year, month, week of month and Day of week ? C# 如何找到过去一周的日期范围? 周日至周六,但不得超过所属月份 - How to find the date range of the past week? Sunday to Saturday but should not exceed the month it belongs to 使用C#中的按钮从月份日历中获取文本框中的日期 - Get Date in Text Box from month calendar with button in c# 如何获取每月日历视图的第一天和最后一天(周日至周六) - How to get the first and last day of a month calendar view (sunday-saturday) 使用月份确定日期的简单公式? - Simple formula for determining date using week of month?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM