I have a query regarding to find out which is day (in given list) is nearest day from today.
For example
day_list is Mon, Tue, Fri, Sat
and today is Wed
Now If we calculate days on paper,
Wed to Mon = 5 days
Wed to Tue = 6 days
Wed to Fri = 2 days
Wed to Sat = 3 days
So answer will be Fri .
But in code, how can I implement this logic? Or is there any other way to optimize its process (regarding if_else)?
What I have in mind is below but probably you should change a little bit to fit your need.
Give your days of week number from 0 to 6.
Sun = 0; Mon = 1; Tue = 2; etc.If you just have some of days, keep the numbers. For example, if you have only
Mon,Tue,Fri, andSatthen keep the number respective to the days, i.e.1,2,5, and6.Now, if you want to know the “distance” between those days from
Wed(which has a number3), you can do this math:distance = (theNumberOfYourDayInList - 3) mod 77is the number of the days, by the way.So if you want to know the distance between:
WedtoMon:distance = (1 - 3) mod 7which is5.WedtoFri:distance = (4 - 3) mod 7which is1.WedtoTue:distance = (2 - 3) mod 7which is6.Turn those logic into codes are trivial and I think you shouldn’t face any problems.