在C#中获取最后/下周的星期三日期

时间:2013-12-05 18:15:37

标签: c#

我怎样才能在周三和下周周三的C#中找到日期:

public Form1()
{
   InitializeComponent();
   CurrentDate.Text = "Today's Date: " + DateTime.Now.ToString("dd/MM/yyyy");
   CurrentRent.Text = "Current Rent Date: "; // last wednesday
   NextRent.Text = "Next Rent Date: "; // next wednesday
}

13 个答案:

答案 0 :(得分:32)

要查找下周三,请继续添加天数,直至找到。要查找上一个星期三,只需减去天数,直到找到一个。

DateTime nextWednesday = DateTime.Now.AddDays(1);
while (nextWednesday.DayOfWeek != DayOfWeek.Wednesday)
    nextWednesday = nextWednesday.AddDays(1);
DateTime lastWednesday = DateTime.Now.AddDays(-1);
while (lastWednesday.DayOfWeek != DayOfWeek.Wednesday)
    lastWednesday = lastWednesday.AddDays(-1);

答案 1 :(得分:5)

假设你是星期三,那么

DateTime.Now.AddDays(7)DateTime.Now.AddDays(-7)是你如何做算术的。如果不是,那么您需要做的是使用DayOfWeek属性来确定确定哪一天是“星期三”所需的天数(正数和负数)。然后,您可以将该值传递到AddDays

例如,如果今天是星期二,你将在上周三AddDays(-6)和下周三AddDays(8)

我会告诉你计算这些的任务。

答案 2 :(得分:3)

使用AddDays例程:

        // increment by the number of offset days to get the correct date
        DayOfWeek desiredDay = DayOfWeek.Wednesday;
        int offsetAmount = (int) desiredDay - (int) DateTime.Now.DayOfWeek;
        DateTime lastWeekWednesday = DateTime.Now.AddDays(-7 + offsetAmount);
        DateTime nextWeekWednesday = DateTime.Now.AddDays(7 + offsetAmount);

应该这样做!

注意:如果是星期一,“上周三”会给你发生的最后一个星期三,但是“下周三”会给你9天后的星期三!如果您希望在两天内获得星期三,则需要使用“%”运算符。这意味着第二个“nextweek”语句将显示为“(7 + offsetAmount)%7”。

答案 3 :(得分:2)

您可以使用它来计算它:

DateTime day = DateTime.Today;
while (day.DayOfWeek != DayOfWeek.Wednesday)
    day = day.AddDays(-1);
var currentRent = day;
var nextRent = day.AddDays(7);

请注意,如果今天是星期三,则今天会显示currentRent,而今天不会显示nextRent。如果你想要反转,你可以颠倒逻辑。

DateTime day = DateTime.Today;
while (day.DayOfWeek != DayOfWeek.Wednesday)
    day = day.AddDays(1);
var currentRent = day.AddDays(-7);
var nextRent = day;

答案 4 :(得分:1)

您需要使用DayOfWeek枚举以及switch语句的if-else结构来确定添加/减去日期的天数。这是繁琐的编码,但很简单。

DateTime nextRent;
DateTime lastRent;
DateTime today = DateTime.Now;

if (today.DayOfWeek == DayOfWeek.Wednesday)
{
   nextRent = today.AddDays(7);
   lastRent = today.AddDays(-7);
}
else if (today.DayOfWeek == DayOfWeek.Thursday)
{
   nextRent = today.AddDays(6);
   lastRent = today.AddDays(-8);
}
//ect for all days

答案 5 :(得分:1)

这会奏效。您需要计算您提供的日期与最近的星期三之间的天数差异,并根据差异是否大于零来计算上一个/下个星期三。

int difference = date.DayOfWeek - DayOfWeek.Wednesday;
DateTime lastWednesday = difference > 0 ? date.AddDays(-1 * difference) : date.AddDays(-1 * (7 + difference));
DateTime nextWednesday = lastWednesday.AddDays(7);

答案 6 :(得分:1)

根据Servy的回答,这是一个扩展方法,该方法将返回所需的日期/日期:

    public static DateTime GetPrevious(this DateTime date, DayOfWeek dayOfWeek)
    {
        var lastDay = date.AddDays(-1);
        while (lastDay.DayOfWeek != dayOfWeek)
        {
            lastDay = lastDay.AddDays(-1);
        }

        return lastDay;
    }

    public static DateTime GetNext(this DateTime date, DayOfWeek dayOfWeek)
    {
        var nextDay = date.AddDays(+1);
        while (nextDay.DayOfWeek != dayOfWeek)
        {
            nextDay = nextDay.AddDays(+1);
        }

        return nextDay;
    }

答案 7 :(得分:1)

某些答案的问题之一是DayoyOfWeek是一个值为0-6的枚举。如果一周的后一天等于星期日(枚举值= 0),而您给出了星期二(枚举值= 2),而您想要下一个星期二,则仅执行计算就会出错。

我在自己正在处理的项目中使用了两种方法供自己使用的类。

public static class DateHelper
{
    public static DateTime GetDateForLastDayOfWeek(DayOfWeek DOW, DateTime DATE)
    {
        int adjustment = ((int)DATE.DayOfWeek < (int)DOW ? 7 : 0);
        return DATE.AddDays(0- (((int)(DATE.DayOfWeek) + adjustment) - (int)DOW));
    }

    public static DateTime GetDateForNextDayOfWeek(DayOfWeek DOW, DateTime DATE)
    {
        int adjustment = ((int)DATE.DayOfWeek < (int)DOW ? 0 : 7);
        return DATE.AddDays(((int)DOW) - ((int)(DATE.DayOfWeek)) + adjustment);
    }
}

XUnit测试证明了上面的代码有效。

public class DateHelperUnitTests
{

    [Theory]
    [InlineData(2020, 1, 7, 2020, 1, 7)]
    [InlineData(2020, 1, 8,  2020, 1, 7)]
    [InlineData(2020, 1, 9,  2020, 1, 7)]
    [InlineData(2020, 1, 10, 2020, 1, 7)]
    [InlineData(2020, 1, 11, 2020, 1, 7)]
    [InlineData(2020, 1, 12, 2020, 1, 7)]
    [InlineData(2020, 1, 13, 2020, 1, 7)]
    [InlineData(2020, 1, 14, 2020, 1, 14)]
    [InlineData(2020, 1, 15, 2020, 1, 14)]
    public void GetDateForLastDayOfWeek_MultipleValues_Pass(
        int InputYear, int InputMonth, int InputDay,
        int ExpectedYear, int ExpectedMonth, int ExpectedDay)
    {
        DateTime DateToTest = new DateTime(InputYear, InputMonth, InputDay);
        DateTime NewDate = DateHelper.GetDateForLastDayOfWeek(DayOfWeek.Tuesday, DateToTest);
        DateTime DateExpected = new DateTime(ExpectedYear,ExpectedMonth,ExpectedDay);

        Assert.True(0 == DateTime.Compare(DateExpected.Date, NewDate.Date));
    }

    [Theory]
    [InlineData(2020, 1, 7, 2020, 1, 14)]
    [InlineData(2020, 1, 8, 2020, 1, 14)]
    [InlineData(2020, 1, 9, 2020, 1, 14)]
    [InlineData(2020, 1, 10, 2020, 1, 14)]
    [InlineData(2020, 1, 11, 2020, 1, 14)]
    [InlineData(2020, 1, 12, 2020, 1, 14)]
    [InlineData(2020, 1, 13, 2020, 1, 14)]
    [InlineData(2020, 1, 14, 2020, 1, 21)]
    [InlineData(2020, 1, 15, 2020, 1, 21)]
    public void GetDateForNextDayOfWeek_MultipleValues_Pass(
        int InputYear, int InputMonth, int InputDay,
        int ExpectedYear, int ExpectedMonth, int ExpectedDay)
    {
        DateTime DateToTest = new DateTime(InputYear, InputMonth, InputDay);
        DateTime NewDate = DateHelper.GetDateForNextDayOfWeek(DayOfWeek.Tuesday, DateToTest);
        DateTime DateExpected = new DateTime(ExpectedYear, ExpectedMonth, ExpectedDay);

        Assert.True(0 == DateTime.Compare(DateExpected.Date, NewDate.Date));
    }
}

答案 8 :(得分:0)

来自Servy的改进答案。您需要在while循环中将nextWednesday或lastWednesday日期实际设置为新日期,否则它将进入无限循环

DateTime nextWednesday = DateTime.Now.AddDays(1);
while (nextWednesday.DayOfWeek != DayOfWeek.Wednesday)
    nextWednesday = nextWednesday.AddDays(1);
DateTime lastWednesday = DateTime.Now.AddDays(-1);
while (lastWednesday.DayOfWeek != DayOfWeek.Wednesday)
    lastWelastWednesday.AddDays(-1);

答案 9 :(得分:0)

您可以创建2个DateTime扩展方法,可以与DayOfWeek参数一起使用:

public static class DateTimeExtension
{
    public static DateTime GetPreviousWeekDay(this DateTime currentDate, DayOfWeek dow)
    {
        int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
        return currentDate.AddDays(-7).AddDays(gotoDay-currentDay);
    }

    public static DateTime GetNextWeekDay(this DateTime currentDate, DayOfWeek dow)
    {
        int currentDay = (int)currentDate.DayOfWeek, gotoDay = (int)dow;
        return currentDate.AddDays(7).AddDays(gotoDay - currentDay);
    }
}

然后可以按如下方式使用:

DateTime testDate = new DateTime(2017, 01, 21);

Console.WriteLine(testDate.GetPreviousWeekDay(DayOfWeek.Wednesday));
Console.WriteLine(testDate.GetNextWeekDay(DayOfWeek.Wednesday));

答案 10 :(得分:0)

    private static DateTime FindPreviousDayOfWeek(DateTime fromDate, DayOfWeek findDay, 
        bool skipSame = false)
    {
        if (fromDate.DayOfWeek < findDay)
            fromDate = fromDate.AddDays(-((int)fromDate.DayOfWeek - 1 + (int)findDay));
        else if (fromDate.DayOfWeek > findDay)
            fromDate = fromDate.AddDays(-((int)fromDate.DayOfWeek - (int)findDay));
        else if (fromDate.DayOfWeek == findDay && skipSame == true)
            fromDate = fromDate.AddDays(-7);

        return fromDate;
    }

如果将saem作为所需的日期,则skipSame变量不包括当前日期。

答案 11 :(得分:0)

这是一线实现的目标。理解它实际上是如何工作的,但确实如此:

下周三获取:

 dt.AddDays(-(int)(dt.AddDays(-4).DayOfWeek) + 6);

上周三获取

 dt.AddDays(-(int)(dt.AddDays(-3).DayOfWeek));

在两种情况下,它都会返回星期三本身的日期。 这适用于任何工作日,只需调整AddDays()调用中的数字即可。 例如星期五:

下周五获得

 dt.AddDays(-(int)(dt.AddDays(-6).DayOfWeek) + 6);

上周五获得

 dt.AddDays(-(int)(dt.AddDays(-5).DayOfWeek));

答案 12 :(得分:0)

此扩展方法应该在每周的任何一天都可以解决问题

   public static class DateTimeExtensions
    {
        public static DateTime LastDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
        {
            return _date.AddDays(-1 * ((_date.DayOfWeek - dayofweek) % 7)).Date;
        }

        public static DateTime NextDayOfWeek(this DateTime _date, DayOfWeek dayofweek)
        {
            return _date.LastDayOfWeek(dayofweek).AddDays(7).Date;
        }
    }

用法

var lastWendsday = DateTime.Now.LastDayOfWeek(DayOfWeek.Wednesday);