如何使用 DayOfWeek 获取之前的日期?

发布于 2024-12-11 20:35:10 字数 868 浏览 0 评论 0原文

我的数据库中有一个提醒表,其中有 2 列用于存储 ActionDay 和 ReminderDay。这些天数都存储为整数: 0 (sun) -> 0 (sun) -> 0 (sun) -> 6(星期六)代表一周中的几天。

当我加载页面时,我想显示下一个 ActionDay 日期以及 ReminderDay 日期。

我可以使用以下方法计算出下一个 ActionDay 日期:

public static class DateTimeExtensions
{
    public static DateTime Next(this DateTime date, DayOfWeek weekday)
    {
        return (from i in Enumerable.Range(0, 7)
                where date.AddDays(i).DayOfWeek == weekday
                select date.AddDays(i)).First();

    }
}

我在计算 ActionDay 的上一个日期时遇到一些问题。

例如,(使用今天的日期 2011 年 10 月 26 日): 如果 ActionDay 是“3”并且 ReminderDay 是“2”那么我期望 行动日为“10 月 26 日星期三”,提醒日为“10 月 25 日星期二”。

如果 ActionDay 是 0 并且 ReminderDay 是 6 我期望 “10 月 30 日星期日”和“10 月 29 日星期六”

对于如何获取此提醒日期有什么建议吗?我也有兴趣知道是否有任何好的 c# 日期时间扩展/片段,因为插入其他人测试过的东西可能比在这里编写自己的东西更容易:)

谢谢, 富有的

I have a reminder table in my database that has 2 columns to store the ActionDay and ReminderDay. These days are both stored as integers: 0 (sun) -> 6 (sat) to represent the days of the week.

When I load my page I'd like to show the next ActionDay date and along with the ReminderDay date.

I can work out the next ActionDay date using this:

public static class DateTimeExtensions
{
    public static DateTime Next(this DateTime date, DayOfWeek weekday)
    {
        return (from i in Enumerable.Range(0, 7)
                where date.AddDays(i).DayOfWeek == weekday
                select date.AddDays(i)).First();

    }
}

I'm having some trouble working out the previous date from the ActionDay.

For example, (using today's date of 26 Oct 2011):
If the ActionDay is '3' and the ReminderDay is '2' then I expect
'Wed, 26 Oct' for the ActionDay and 'Tues, 25 Oct' for the ReminderDay.

If the ActionDay is 0 and the ReminderDay is 6 I'd expect
'Sun, 30 Oct' and 'Sat, 29 Oct'

Any suggestions on how I might be able to get this Reminder date? I'd also be interested to know if there are any good c# datetime extensions/snippets as it might be easier to plug in something that someone else has tested than writing my own here :)

Thanks,
Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2024-12-18 20:35:10

您可以计算 ActionDay 和 ReminderDay 之间的差异,然后对表示操作日的 DateTime 调用 AddDay() 函数来获取提醒日的 ​​DateTime:

// here a stands for the number representing the action day, and r for the reminder day
// a and r are both integers from 0 to 6
int diff;
if (a >= r)
{
    diff = a - r;
}
else
{
    diff = a + 7 - r;
}

DateTime reminderDateTime = actionDateTime.AddDays(-1 * diff);

You can calculate the difference between the ActionDay and ReminderDay and then call the AddDay() function on the DateTime representing the action day to get the DateTime for reminder day:

// here a stands for the number representing the action day, and r for the reminder day
// a and r are both integers from 0 to 6
int diff;
if (a >= r)
{
    diff = a - r;
}
else
{
    diff = a + 7 - r;
}

DateTime reminderDateTime = actionDateTime.AddDays(-1 * diff);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文