简体   繁体   English

如何获取特定星期中给定日期的日期?

[英]How to get the date of a given day in a specific week?

I want to get date of specific day of a given week. 我想获取给定星期几的日期。 The week is decided by the entered date. 星期由输入的日期决定。

For example, if I want the Friday of these dates: 例如,如果我想要以下日期的星期五:

  1. 06/27/2018
  2. 07/04/2018
  3. 07/07/2018

I expect as outcome: 我期望作为结果:

  1. 06/29/2018
  2. 07/06/2018
  3. 07/06/2018

Here, week is defined as Monday to Sunday. 在此,星期定义为星期一至星期日。

You can do this with some clever maths based on the DayOfWeek : 您可以根据DayOfWeek通过一些聪明的数学来做到这一点:

public DateTime GetDay(DateTime source, DayOfWeek dayOfWeek, 
    DayOfWeek weekStartsOn = DayOfWeek.Monday)
{
    var offset = (int)source.DayOfWeek - (int)weekStartsOn;;
    if(offset < 0)
    {
        offset = offset + 7;
    }
    return source.AddDays(-offset + (int)dayOfWeek - (int)weekStartsOn);
}

And use it like this: 并像这样使用它:

var someDate = ...; //Get a date from somewhere

var friday = GetDay(someDate, DayOfWeek.Friday);
var monday = GetDay(someDate, DayOfWeek.Monday);

And if your week starts on a Sunday, just use the optional third parameter, for example: 如果您的一周从星期日开始,则只需使用可选的第三个参数,例如:

var friday = GetDay(someDate, DayOfWeek.Friday, DayOfWeek.Sunday);

A modification of the current version of DavidG's answer: 对DavidG答案的当前版本的修改:

static DateTime GetDay(DateTime source, DayOfWeek dayOfWeek)
{
  const int offsetSinceMondayIsFirstDayOfWeek = 7 - (int)DayOfWeek.Monday;
  return source.AddDays(((int)dayOfWeek + offsetSinceMondayIsFirstDayOfWeek) % 7
    - ((int)source.DayOfWeek + offsetSinceMondayIsFirstDayOfWeek) % 7);
}

This takes into account that the asker considers Monday to be the first day of the week. 这考虑到询问者将星期一视为一周的第一天。 If you want Saturday as the first day of week, just replace Monday with Saturday above. 如果你想周六周的第一天,只需更换MondaySaturday以上。

In the special case where you consider Sunday the first day of the week, it reduces to DavidG's original method: 在特殊情况下,您将星期日视为一周的第一天,它简化为DavidG的原始方法:

static DateTime GetDay(DateTime source, DayOfWeek dayOfWeek)
{
  return source.AddDays((int)dayOfWeek - (int)source.DayOfWeek);
}

because the DayOfWeek enum type of the BCL already "wraps around" between Saturday ( = 6 ) and Sunday ( = 0 ). 因为BCL的DayOfWeek枚举类型已经在Saturday= 6 )和Sunday= 0 )之间“环绕”。


On http://chartsbin.com/view/41671 there is a world map that apparently shows what day of week is considered the first day of the week in different regions. http://chartsbin.com/view/41671上,有一张世界地图,显然显示了在不同地区中一周的第一天。

    public void TestMethod1 ( )
    {

        DateTime date = DateTime.Now;
        DateTime friday = date.AddDays( (int)DayOfWeek.Friday - (int)date.DayOfWeek );
        Console.WriteLine( friday.ToString( ) );

    }
   using System;
      using System.Globalization;

   public class Example
   {
      public static void Main()
      {
         DateTime dateValue = new DateTime(2008, 6, 11);
         Console.WriteLine(dateValue.ToString("ddd", 
                           new CultureInfo("fr-FR")));    
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM