Condition every second week on wednesdays

The following template computes the number of days since the Unix Epoch and divides by 7.

{{ ((now() - '19700101T00Z' | as_datetime).days // 7) is odd }}

However, according to an online resource, there have been 13 leap years since 1970. Merely dividing the total days by 7 doesn’t take leap years into account therefore the computed result is a value that isn’t guaranteed to align with the boundaries of the current calendar week.

Nevertheless, it’s probably ‘good enough’ for this application because it performs the check on a Wednesday (i.e. at the middle of the calendar week and not at its boundaries which aren’t necessarily aligned with the formula’s concept of a week).


EDIT

Correction. Integer division operator is //

1 Like

I think this is the correct approach anyways. Personally, I don’t care if it’s an odd or even week. I just want every other week from when I start doing it. A date input makes sense and it would fire every other week from that date.

I think it should still work.
A leap day does not make the week longer, only the month. :slight_smile:

PS: Leap days are special to me, as that is my birthday :birthday:

1 Like

Today is Monday October 17.

The first template reports the start of the next week is in 7 days.

{{ (now() + timedelta(days=7)).isocalendar().week }}

The second template indicates that the next multiple of 7 days starts in 3 days.

{{ (t + timedelta(days=3)).days // 7 }}

The second template’s result is not aligned with calendar weeks (the Unix Epoch doesn’t start at the beginning of a calendar week plus there are 13 additional days due to leap years; elapsed days since the epoch divided by 7 doesn’t align with calendar weeks). As I mentioned previously, that offset can affect one’s application (it depends on the application).

Lastly, the original example I posted, of the Unix epoch calculation, used the wrong division operator. We want integer division so that’s // not %. I have corrected the original example posted above.


NOTE

If you want to experiment, copy-paste this into the Template Editor.

{{ (now() + timedelta(days=0)).isocalendar().week }}
{{ (now() + timedelta(days=1)).isocalendar().week }}
{{ (now() + timedelta(days=2)).isocalendar().week }}
{{ (now() + timedelta(days=3)).isocalendar().week }}
{{ (now() + timedelta(days=4)).isocalendar().week }}
{{ (now() + timedelta(days=5)).isocalendar().week }}
{{ (now() + timedelta(days=6)).isocalendar().week }}
{{ (now() + timedelta(days=7)).isocalendar().week }}
{{ (now() + timedelta(days=8)).isocalendar().week }} 

{% set t = now() - '19700101T00Z' | as_datetime %}
{{ (t + timedelta(days=0)).days // 7 }}
{{ (t + timedelta(days=1)).days // 7 }}
{{ (t + timedelta(days=2)).days // 7 }}
{{ (t + timedelta(days=3)).days // 7 }}
{{ (t + timedelta(days=4)).days // 7 }}
{{ (t + timedelta(days=5)).days // 7 }}
{{ (t + timedelta(days=6)).days // 7 }}
{{ (t + timedelta(days=7)).days // 7 }}
{{ (t + timedelta(days=8)).days // 7 }}

That is what I believe my template takes care of.
I believe if we set the template correctly now, then it will last for years before needing a calibration again since the week change is in the middle of the night.
I don’t believe too many of us get up at midnight to roll out the bins :wink:

I repeated the test I performed above using your template (adjusted for my timezone) and it starts a new ‘week’ tomorrow (i.e. the calculated value goes from odd to even on Tuesday).

Should be OK for this application because it checks on Wednesdays. However, as I explained previously, because it’s not aligned with a calendar week it may present a problem for some applications.

For example, if one application checks on a Monday and another on a Friday, the template will report odd on Monday and even on Friday despite the fact both days are within the same calendar week. That’s because its concept of a ‘week’, a period spanning 7 days, is not aligned with a calendar week.

Taking under advisement that it may not work for all situations (where the start of the week or year boundary matters)… You can use modulus to do other week skips as well:

# Every 2 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 2 == 0 }}

# Every 3 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 3 == 0 }}

# Every 7 weeks
{{ ((now() - '19700101T00Z' | as_datetime).days // 7) % 7 == 0 }}

1 Like

Yes if there’s a need for skipping more than one given interval of time (is odd is adequate for this particular application).

FWIW, you can shrink the template a bit more for computing every other week (i.e. every two weeks):

{{ ((now() - as_datetime(0)).days // 7) is odd }}

Applying it to your template:

{{ ((now() - as_datetime(0)).days // 7) % 2 == 0 }}
1 Like