Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Finding Next Active Day of Week

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.

like image 306
UtopiaLtd Avatar asked Dec 28 '25 02:12

UtopiaLtd


1 Answers

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.)

like image 122
Jon Skeet Avatar answered Dec 30 '25 15:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!