简体   繁体   English

如何在 C# 中获取下一个特定日期的日期

[英]How to get date of next coming specific day in C#

How can I get the date of next coming Monday or next coming Friday in C#.如何在 C# 中获得下周一或下周五的日期。 lets say today is Wednesday and I want to get date of coming Friday.假设今天是星期三,我想知道即将到来的星期五的日期。 This is what I've done这就是我所做的

DateTime time = DateTime.Now.Date;
DateTime NextFriday;
if (time.DayOfWeek == DayOfWeek.Wednesday)
{
    NextFriday = DateTime.Now.AddDays(2);
}

with this approach I've to initiate 7 variables for each day and 7 conditions for ever day to find the next specific day.使用这种方法,我必须每天启动 7 个变量,每天启动 7 个条件,以找到下一个特定的日子。

Is there any better and clean code by which I can get the date of any of the next coming day.有没有更好更干净的代码可以让我获得下一天的任何日期。

Using the following使用以下

public int CalculateOffset(DayOfWeek current, DayOfWeek desired) {
    // f( c, d ) = [7 - (c - d)] mod 7
    // f( c, d ) = [7 - c + d] mod 7
    // c is current day of week and 0 <= c < 7
    // d is desired day of the week and 0 <= d < 7
    int c = (int)current;
    int d = (int)desired;
    int offset = (7 - c + d) % 7;
    return offset == 0 ? 7 : offset;
}

You can calculate how far you are from the desired day and then add that to the current date您可以计算距离所需日期的距离,然后将其添加到当前日期

DateTime now = DateTime.Now.Date;
int offset = CalculateOffset(now.DayOfWeek, DayOfWeek.Friday);
DateTime nextFriday = now.AddDays(offset);
DateTime today = DateTime.Today;
DateTime nextFriday = System.Linq.Enumerable.Range(0, 6)
  .Select(i => today.AddDays(i))
  .Single(day => day.DayOfWeek == DayOfWeek.Friday);

You should probably use a time library that supports this, such as NodaTime.您可能应该使用支持此功能的时间库,例如 NodaTime。

See date.Next(IsoDayOfWeek.Sunday) on https://nodatime.org/1.3.x/userguide/arithmetic请参阅 https 上的date.Next(IsoDayOfWeek.Sunday) ://nodatime.org/1.3.x/userguide/arithmetic

Here's an alternative solution (please don't use this):这是一个替代解决方案(请不要使用它):

DateTime F(DateTime t, DayOfWeek dayOfWeek) => t.AddDays((7 + (int)dayOfWeek - (int)t.DayOfWeek) % 7);

for (int i = 0; i < 7; i++)
    Console.WriteLine((DayOfWeek)i + " " + F(DateTime.Now, (DayOfWeek)i));

Outputs (on Wednesday 4/25/2018):产出(2018 年 4 月 25 日星期三):

Sunday 4/29/2018 12:00:00 AM
Monday 4/30/2018 12:00:00 AM
Tuesday 5/1/2018 12:00:00 AM
Wednesday 4/25/2018 12:00:00 AM
Thursday 4/26/2018 12:00:00 AM
Friday 4/27/2018 12:00:00 AM
Saturday 4/28/2018 12:00:00 AM

DayOfWeek is just an enum between 0 and 6, so with modular arithmetic you can use the difference between your date of interest and target day of week to compute the number of days you must add. DayOfWeek 只是一个介于 0 和 6 之间的枚举,因此通过模运算,您可以使用您感兴趣的日期和目标星期几之间的差异来计算您必须添加的天数。

A quick warning, you need to take into account timezone of interest when you ask what is meant by "today".一个快速警告,当您询问“今天”的含义时,您需要考虑感兴趣的时区。 It means a different thing depending on which side of the date line you live.这取决于您住在日期变更线的哪一侧,这意味着不同的事情。

using System;

public class Program
{

    public static DateTime NextDayForDay(DayOfWeek dayOfWeek, DateTime occurringAfter)
    {
        return occurringAfter.AddDays(((dayOfWeek - occurringAfter.DayOfWeek + 6) % 7)+1); 
    }

    public static void Main()
    {
        for (int i=0; i < 7; i++)
        {
            for (int j=0; j < 7; j++)
            {
                DayOfWeek dayOfWeek = (DayOfWeek)(((int)DayOfWeek.Sunday + j) % 7);

                DateTime test = DateTime.Today.AddDays(i);
                Console.WriteLine($"{test}=>Next {dayOfWeek} is {NextDayForDay(dayOfWeek, test)}");
            }
        }   
    }
}

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

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