Button in date picker to show the last day of the current month?

In a tiddler I have a date picker using

<div style="margin-bottom: 15px; padding-bottom: 15px; border-bottom: 1px solid #eee;">
  <strong>From:</strong> <$edit-text tiddler="$:/state/range" field="start" type="date"/>
  &nbsp;&nbsp;
  <strong>To:</strong> <$edit-text tiddler="$:/state/range" field="end" type="date"/>
</div>

and a button as follows:

   <$button class="tc-btn-invisible" style="border: 1px solid #ccc; padding: 2px 8px; border-radius: 4px; margin-right: 5px;">
      This Month
<$action-setfield $tiddler="$:/state/range" start=<<now "YYYY-0MM-01">> end=<<now "YYYY-0MM-0DD">> />
    </$button>

Is it possible for the button to show the last day of the current month instead of today’s date in the “To” date field of the date picker?

Try this:

<$let last={{{ [<now YYYY>remainder[4]match[0]then<now MM>match[2]then[29]]
            ~[[31 28 31 30 31 30 31 31 30 31 30 31]split[ ]nth<now MM>] }}}>
<$button class="tc-btn-invisible" style="border: 1px solid #ccc; padding: 2px 8px; border-radius: 4px; margin-right: 5px;">
   This Month
   <$action-setfield $tiddler="$:/state/range"
      start=<<now "YYYY-0MM-01">> end={{{ [<now "YYYY-0MM-">addsuffix<last>] }}}/>
</$button>

Notes:

  • The last calculation uses filter syntax to calculate the number of days in the current month.
    • The first filter run says “if it is a leap year and the current month is February, then there are 29 days this month”
    • The second filter run says, “otherwise, split a space-separated list of days-per-month into individual values, and use the value for the current month”
  • Then, when setting the “end” field value, {{{ [<now "YYYY-0MM-">addsuffix<last>] }}} constructs the date string by appending the calculated “last day” value to the current “year-month-”

enjoy,
-e

2 Likes

A truly ingenious solution. Amazing how you translate the logic into code and vice versa.

1 Like