Date Calculator - Calendar Days and Week Days

Calculate number of calendar days or week days between two dates.
Works even across multiple years and leap years.

{note: trim=right}
From: {formdate: YYYY-MM-DD; name=from; default=2020-02-28}
To: {formdate: YYYY-MM-DD; name=to; default=2020-03-01}
{if: {time: YYYY; at={=to}}=={time: YYYY; at={=from}}; trim=right}
{caldiff={time: DDDD; at={=to}}-{time: DDDD; at={=from}}}
{elseif: {time: YYYY; at={=to}}-{time: YYYY; at={=from}}==1; trim=right}
{caldiff=({time: DDDD; at={=to}}-{time: DDDD; at={=to}; shift=<Y})+({time: DDDD; at={=from}; shift=>Y}-{time: DDDD; at={=from}}+1)}
{elseif: {time: YYYY; at={=to}}-{time: YYYY; at={=from}}>1; trim=right}
{caldiff=sum(map(seq({time: YYYY; at={=from}}+1, {time: YYYY; at={=to}}-1), item -> {time: DDDD; at={=item}; pattern=YYYY; shift=>Y}))+({time: DDDD; at={=to}}-{time: DDDD; at={=to}; shift=<Y})+({time: DDDD; at={=from}; shift=>Y}-{time: DDDD; at={=from}}+1)}
{endif}
{endnote: trim=right}

{if: {time: x; at={=to}} < {time: x; at={=from}}; trim=right}
The "From" date must be earlier than the "To" date.
{else: trim=right}
Between {time: dddd, MMMM Do, YYYY; at={=from}} and {time: dddd, MMMM Do, YYYY; at={=to}} there {if: caldiff <> 1}are {else}is {endif: trim=right} {=caldiff} calendar day{if: caldiff <> 1}s.{endif}
{endif}

1 Like