[tw5] ISO week date format template

I would like to use the ISO week date format for the title of new journal tiddlers and elsewhere, since I find it practical to be aware of the week day in combination with a sortable date format.

YYYY-Www-D or YYYYWwwD

E.g. 2021-w36-4 for thursday of week 36, 2021-09-09.

I can not see a way to get the week day number in the built in date format tokens.

https://tiddlywiki.com/#DateFormat

The closest I’ve got is YYYY-W0WW-ddd, but that renders as 2021-W36-Thu, which will not sort alphanumerically in date order like an ISO date would.

I can see that the template tokens are implemented in a system tiddler named $:/core/modules/utils/utils.js.

Getting the day of week is trivial in JavaScript, e.g. adding a token matching a single “D” for this:

[/^D/, function() {
return date.getDay() || 7;
}]

The utils.js tiddler seems to contain a lot of other stuff however, and I guess local changes would have to be merged on achieving updates of TiddlyWiki.

Maybe there are other ways to achieve custom date formatting of this kind in TiddlyWiki?

A correction of the example given for a week date in TiddlyWiki is that the template should use the wYYYY token for the ISO week-numbering year, not YYYY, since weeks may cross the traditional year boundary with some days of the week falling outside the year the week belongs to. It is a bit surprising that the final element of the week day number is missing when the other parts of the ISO standard are supported.

torsdag 9 september 2021 kl. 21:31:46 UTC+2 skrev J N:

1 Like

G’day,

Just to get the conversation started, try this in a new tiddler in some tiddlywiki:
<$vars dateNow= <<now wYYYY-W0WW-ddd>>>
{{{ [<dateNow>search-replace:[Mon],[1]search-replace:[Tue],[2]search-replace:[Wed],[3]search-replace:[Thu],[4]search-replace:[Fri],[5]search-replace:[Sat],[6]search-replace:[Sun],[7]] }}}
</$vars>

Oops, typo in the last "search-replace"

“[Thu],[7]” should be “[Sun],[7]”

The dangers of mindless cut and paste …

Thank you for the suggested workaround.

Is it possible to define that operation in a tiddler and then refer to the value in a template for creating a new journal entry?

If so, maybe one could also use JavaScript directly to provide the value for the tiddler template.

lördag 11 september 2021 kl. 15:19:35 UTC+2 skrev cj.v:

Thank you for the suggested workaround.

Is it possible to define that operation in a tiddler and then refer to the value in a template for creating a new journal entry?

If so, maybe one could also use JavaScript directly to provide the value for the tiddler template.

Thanks CJ for the cunning workaround. For v5.2.0 I’ve now added a new date format string token “dddd” for directly obtaining the ISO weekday number (ie 1=Monday, 7=Sunday):

https://tiddlywiki.com/prerelease/#DateFormat

(It’ll take a few minutes from this posting until the change shows up).

Best wishes

Jeremy.

Thanks Jeremy,

I did just have a look at the code to create a PR. You have been faster to fix it as I’ve been looking up the code :wink:
-m

I’m thinking it might be worth you using the 5.2 pre-release version of TiddlyWiki, seeing as it has the ISO date format.

However, if you do want to continue with the workaround:

I wouldn’t bother with javascript. You have all you need in TiddlyWiki: https://tiddlywiki.com/#Making%20a%20custom%20journal%20button

What do you think?

1 Like

Nice to hear it is already taken care of, thank you.
I was almost on my way to make a pull request myself, as an exercise in contributing to an open source project, but I am not sure about the customs, if I should create an issue first.

Of curiosity, may I ask why you chose “dddd” as the token for week date number, instead of the “D” symbol used in the ISO 8601 formats notation?

1 Like

Yes, I will probably upgrade to 5.2 then to get that specific feature.

Thank you for the doc ref, there seem to be many possibilities to customize functionality.

The last block of example code means that I should create the config tiddlers without the $:/ prefix if I want them to appear in the sidebar UI?

Nice to hear it is already taken care of, thank you.
I was almost on my way to make a pull request myself, as an exercise in contributing to an open source project, but I am not sure about the customs, if I should create an issue first.

It’s probably usually best to open an issue (or comment on an existing one) for discussion before preparing a PR to minimise the chances of it subsequently being rejected for a reason you haven’t foreseen. We are always keen to welcome new contributors, and you’ll find plenty of help and encouragement when you need it.

Of curiosity, may I ask why you chose “dddd” as the token for week date number, instead of the “D” symbol used in the ISO 8601 formats notation?

We don’t currently use any single character tokens, originally to minimise the chances of triggering any boilerplate text in the template (ie, a format string could be “The year is YYYY”).

Now that I started to write out the rationale for the choice of “dddd”, I’m realising that perhaps I think “dd” might be a better choice. What do others think?

Best wishes

Jeremy

Ok, I see that there is also already a conflict with the ISO notation in the DDD token, which in ISO 8601 is used for three digit day of year.
Maybe that could be nice to have as TW date token also.

As represented above, [YYYY] indicates a year. [DDD] is the day of that year, from 001 through 365 (366 in leap years). For example, “1981-04-05” is also “1981-095”.

The “dd” format is reminding of the numerical notation in the month date, but at the same time may be confusingly similar and suggesting a two digit representation. “dddd” feels more related to the existing weekday name notations, one could think that longer token indicates lesser units in this case. I’m not sure which one to prefer.
How could a day of year-token fit into this? yDDD?

I think 0dd and dd should be an option. I think 0dd is needed to get easy default sort order.

-mario

The “dd” format is reminding of the n umerical notation in the month date, but at the same time may be confusingly similar and suggesting a two digit representation. “dddd” feels more related to the existing weekday name notations, one could think that longer token indicates lesser units in this case. I’m not sure which one to prefer.

That’s roughly my thinking too. In particular, there is a precedent for longer tokens generating shorter substitutions.

How could a day of year-token fit into this? yDDD?

If we stick with dddd for day of week, perhaps we’d use ddddd for day of year? Perhaps that’s excessively long, but it does seem like a very unusual requirement in any case; only users who were looking for it would want it, if you see what I mean.

I think 0dd and dd should be an option. I think 0dd is needed to get easy default sort order.

The day of week is a number from 1 to 7, so zero padding doesn’t seem useful.

Best wishes

Jeremy

If we stick with dddd for day of week, perhaps we’d use ddddd for day of year?

They are very similar. For me it’s difficult to recognise a difference. …

Perhaps that’s excessively long, but it does seem like a very unusual requirement in any case; only users who were looking for it would want it, if you see what I mean.

You are right. Only users that search for it will use it.

I think 0dd and dd should be an option. I think 0dd is needed to get easy default sort order.

The day of week is a number from 1 to 7, so zero padding doesn’t seem useful.

Right. My bad.

But ddddd day of year may have 1 or 2 leading zeros. …

-m

OK, I’m thinking now:

  • To change the token for week number to dd
  • To add a new token for day of year dddd and 0dddd

Best wishes

Jeremy

Sounds fine to me.
The zero-padded format is probably the common case for the day of year.
“yDDD” would be the more shape-similar and mnemonic, but it’s already in the proposals and maybe it breaks away from existing patterns.
The main thing is that the ISO date parts are represented and available.

1 Like