C# 查找一周中的下一个活跃日

发布于 2024-12-07 05:19:28 字数 286 浏览 0 评论 0原文

我正在使用一个 C# 类来表示一个配置文件,其中包含一些计划信息,指示一天中的更新次数,以及一周中每一天的布尔值更新的日期。我在这里和其他网站上看到了一些帖子,以查找给定工作日的下一个实例等,但没有找到可以改变的日期。

例如,我可能会得到一个星期一、星期三和星期四为 true 的对象。查找一周中下一个真实日期的下一个实例的最佳方法是什么?

我可以想出一些漫长而漫长的方法来找到一周中的下一个“真正”的一天,但是是否有一些我不熟悉的更干净的内置方法可以做到这一点?我不想使用任何第三方库,因为这需要信息保证人员的大量特别批准。

I've got a C# class I'm working with representing a profile that includes some scheduling information indicating how many updates during the day, and which days to update with boolean values for each day of the week. I've seen some posts here and on other sites to find the next instance of a given weekday, etc. but not something where which days one is looking for can vary.

For example, I might be given an object with Monday, Wednesday, and Thursday as true. What's the best way to find the next instance of the next true day of the week?

I can think of some long and drawn out ways to find the next "true" day of the week, but is there something cleaner built in that I'm unfamiliar with that would do the trick? I'd prefer not to use any third party libraries, as that requires lots of special approval from information assurance folks.

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

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

发布评论

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

评论(1

ゞ记忆︶ㄣ 2024-12-14 05:19:28

鉴于循环几乎不会花费很长时间,这将是最简单的方法:(

DateTime nextDay = currentDay.AddDays(1);
while (!profile.IsActive(nextDay.DayOfWeek))
{
    nextDay = nextDay.AddDays(1);
}

假设您已经验证该配置文件至少在一天内处于活动状态......否则它将永远循环。)

Given that it's hardly going to take a long time to loop, that's going to be the simplest approach:

DateTime nextDay = currentDay.AddDays(1);
while (!profile.IsActive(nextDay.DayOfWeek))
{
    nextDay = nextDay.AddDays(1);
}

(That's assuming you've already validated that the profile is active on at least one day... otherwise it'll loop forever.)

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