Filter to get last weeks journal entries with links

I record a daily journal using the Journal button which makes a new tiddler with the “Journal” tag. I was interested in making a weekly summary but realized that filter for that would be very difficult.

Does anyone know how I could make a filter that will show the tiddlers tagged as Journal created last week–that is monday - friday of last week thus the relative time period only changes once a week?

I’ve poked around and I am fairly sure it’s next to impossible without using JavaScript. Let’s see what we’d need to get it done:

  • Determine the date of last Monday
    • You can get it by determining Monday in current week and subtract 7
    • You could use <<now "dddd">> to get 1-indexed day of the week. Subtract 1 from it and you have the number of days you have to subtract from current date to get Monday. Let’s call it SUBTRACTO
    • You could use <<now "DD">> to get current day of the month. If you subtract SUBTRACTO from it, you will have the day of the month for current Monday.
    • Great! Except what if it results in 0 or a negative number? Then you could use <<now "MM">> and subtract 1 from it to rollunder the month and then…
    • Then you need a lookup table of days in a month (but also keep in mind to check the year for leap years for February). Start from that value and subtract the number from previous point to get the day of the month.
    • Repeat for year rollunder, in case you started in January…
    • Then you’d need to repeat the whole process, subtracting 7 more from that.
    • Then convert both to TW’s modified/created format.

It’s all technically possible but because TW’s filters don’t support conditionals whatever you end up with is going to be a monster of a filter :slight_smile:.

Maybe I could make a custom filter in JS for this purpose.

The better question is, if I know the timestamp range can I ask the current filter operations to filter by that timestamp range?

Well I guess if I’m making my own filter in JS I could do that logic there as well and just return the set of titles that match.

Keep in mind there is a week number date format and you can test it for each date the week number it has. Then you can also test day of week to seperate weekends.

The days operator can be used for last 7 days as can be the use of unix time.

More one or more days operators can generate ranges.

Also so long as you handle year changes, day of year is useful as well.

EDIT: Oops, apologies, Tones. I hit the wrong reply button.

Try this at TiddlyWiki.com to see the list of tiddlers modified in the last 50 days:

<$let SevenDays={{{ [[50]multiply[24]multiply[60]multiply[60]multiply[1000]] }}}
         today={{{ [<now "TIMESTAMP">] }}}
         backto={{{ [<today>subtract<SevenDays>] }}}
         myfilter="[get[modified]format:date[TIMESTAMP]compare:number:gteq<backto>]">

<$list filter="[all[tiddlers]filter<myfilter>]">

<$link/>
</$list>

</$let>

It might be possible to simplify things by not converting dates to timestamps. I had no time to play around with that this morning.

I did have time for the sample code above, which may give you some ideas on how to filter to get last week’s journal entries.

Oops number 2: forgot to change the variable name"SevenDays" (which was fine for my TiddlyWiki) to “ThisManyDays” for TiddlyWiki.com playing.

I can do it in JS… although with a full week, monday - sunday. I don’t know how to do it in wikitext except to try to follow @Maurycy’s steps above.

We can use something like this:

/*\
title: $:/my/modules/filters/priorweek.js
type: application/javascript
module-type: filteroperator

Filter operator that selects tiddlers with a created date field in the week preceding today (or other given number of weeks prior).

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Export our filter function
*/
exports.priorweek = function(source,operator,options) {
	var results = [],
        fieldDay = null,
		fieldName = (operator.suffixes || [])[0] || "created",
        weeks = $tw.utils.parseNumber(operator.operand || "1"),
        now = new Date(),
		startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() - (7 * weeks - 1 + ((now.getDay() % 7) || 7))),
        endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7 - (7 * weeks - 1 + ((now.getDay() % 7) || 7)));
	source(function(tiddler,title) {
		if(tiddler) {
            fieldDay = tiddler.getFieldDay(fieldName);
			if(fieldDay >= startDate && fieldDay < endDate) {
				results.push(title);
			}
		}
	});
	return results;
};

})();

and use it like this:

<<list-links filter:"[tag[Journal]priorweek[]!sort[created]]">>

for last weeks tiddlers (by created date)

or

<<list-links filter:"[tag[Journal]priorweek[2]!sort[created]]">>

for the week before last, and so on.

Or you could choose to use the modified field by adding a suffix:

<<list-links filter:"[tag[Journal]priorweek:modified[]!sort[modified]]">>

To use it, you can download this and drag the resulting file onto your wiki: priorweek.json (1.3 KB) You will have to save and reload before it takes effect.