Is it possible to use days operator inside the value of a field?

Hi,

I was wondering if it is possible to use days operator inside the value of a field? For example, I would like to have a field value that points to X days before current date.

For example if I put days:created[-30] as a field value, that is interpreted as literal days:created[-30]. The use case is I would like to have a tiddler that always have “date_open” field that has value of 30 days before current date.

The days filter operator doesn’t compute a date value. Rather, it finds the tiddler titles whose specified field (e.g.,created) is the indicated number of days relative to the current date.

If you want to calculate a date value relative to the current date, then you could use some wikitext code like this:

<$let days={{{ [[-30]multiply[86400]multiply[1000]] }}}>
{{{ [<now "YYYY0MM0DD">format:date[TIMESTAMP]add<days>format:timestamp[YYYY0MM0DD]] }}}
</$let>

Notes:

  • To get a result that is BEFORE the current date, use a negative number (i.e., -30) in the days calculation.
  • There are 86400 second in a day, so we multiply the date offset (-30) to convert it from days to seconds… but the actual value we need to use is in MILLIseconds, so we multiply that result by 1000.
  • The format:date[TIMESTAMP] filter operator converts a YYYYMMDD date string into a Unix epoch date value (number of milliseconds since January 1, 1970).
  • We can then add the desired number of days (in milliseconds, as calculated above) and then use format:timestamp[YYYY0MM0DD] to convert this result back to a date string.

However, it not advisable (nor practical) to enter complex wikitext as a field value. Instead, it would be more useful (and MUCH simpler) to store the calculated YYYYMMDD date string in the date_open field. But that would result in a fixed date, rather than a date relative to the current date.

While this might be suitable for some use-cases, you indicated that you want a date relative to the current date. So, instead of storing either the complex wikitext code or a calculated date string, what if you just store the desired date offset (i.e., “-30”) in the date_open field? Then, in your display handling, you could calculate the desired “date_open” YYYYMMDD date string on the fly… and, to make your display code cleaner (and more consistent), you could put this calculation into a \procedure definition, like this:

\procedure show_date_open()
<$let days={{{ [{!!date_open}multiply[86400]multiply[1000]] }}}>
{{{ [<now "YYYY0MM0DD">format:date[TIMESTAMP]add<days>format:timestamp[YYYY0MM0DD]] }}}
</$let>
\end

which you would invoke in your display code as just: <<show_date_open>>.

I hope this all makes sense to you. Let me know how it goes…

enjoy,
-e

4 Likes

Ah thank you so much for detailed response. Let me try these!