Day Number

135 views
Skip to first unread message

Ed Heil

unread,
Aug 27, 2020, 3:04:00 PM8/27/20
to TiddlyWiki

Hi,

I'm interested in populating a field on a tiddler with the "day number" (Jan 1 = 1, Dec 31 = 365 or 366).  There doesn't seem to be an option in the now macro which will give me that number, unless I'm missing something. 

I was thinking this might be a good reason to create my first javascript macro, but poking around the documentation, it's not entirely clear to me how it's possible to create a little one-off javascript macro in a tiddler to use in your wiki, the way you can create a little one-off wikitext macro.

I was looking at this:


but it talks about developing javascript macros as "modules."  I'm not sure how to add a new module in my wiki... I see there are existing javascript macro modules which are just tiddlers in $:/core/modules/macros  ... they look just like some javascript in a tiddler...  could I make something like that?  If so, how?

I know Javascript, I just am missing some very simple first steps as to how to get started on this project.

Of course, if there's some way to get a day number out of the now macro, I wouldn't have to worry about it. :)


Eric Shulman

unread,
Aug 27, 2020, 4:25:05 PM8/27/20
to TiddlyWiki
On Thursday, August 27, 2020 at 12:04:00 PM UTC-7, Ed Heil wrote:
I'm interested in populating a field on a tiddler with the "day number" (Jan 1 = 1, Dec 31 = 365 or 366).  There doesn't seem to be an option in the now macro which will give me that number, unless I'm missing something.  
Of course, if there's some way to get a day number out of the now macro, I wouldn't have to worry about it. :)

Here's a macro that calculates the day number (1 through 365/366) and a $button that stores the result in a tiddler field:

\define getDay()
\whitespace trim
<$vars yyyy=<<now "YYYY">> mm=<<now "MM">> dd=<<now "DD">>>
<$set name="dpm" filter="[<yyyy>divide[4]split[.]count[]match[1]]"
     value
="31 29 31 30 31 30 31 31 30 31 30 31" emptyValue="31 28 31 30 31 30 31 31 30 31 30 31">
{{{ [enlist:raw<dpm>first<mm>butlast[]sum[]add<dd>] }}}
\end

<$button> set days
<$wikify name="days" text=<<getDay>>>
<$action-setfield days=<<days>> />
</
$wikify>
</$button>

The getDay() macro does this:
* \whitespace trim removes all whitespace from the macro output
* $vars gets the current year (YYYY), month (MM) and date (DD)
* $set name="dpm" (days per month) determines if the year is a leap year (i.e., evenly divisible by 4) and then defines a list of days per month
* the inline filter enlists the days per month (using :raw to include duplicate values)
* then it trims the list to only include the months up to, but not including, the current month
* and then sums those numbers and adds the date for the current month

The $button does this:
* $wikify takes the getDay() macro and converts it's output to plain text
* $action-setfield saves the plain text value to the "days" field of the current tiddler

enjoy,
-e

TW Tones

unread,
Aug 27, 2020, 7:08:23 PM8/27/20
to TiddlyWiki
Ed et al.

For clarity we would call this the "day of year number" DOY

as opposed to Day of Month DOM, or Week of Year WOY
but you can also have end of day EOD Beginning of day BOD, 

From this follows many things BOW, EOM, EOY BOQ/EOQ

Regards
Tony

Eric Shulman

unread,
Aug 27, 2020, 7:52:50 PM8/27/20
to TiddlyWiki
On Thursday, August 27, 2020 at 4:08:23 PM UTC-7, TW Tones wrote:
For clarity we would call this the "day of year number" DOY

Based on my recent work with time and date (http://TiddlyTools.com/timer.html), I have learned quite a bit about date nomenclature.

In fact, there is a well-defined international standard, ISO-8601 (https://en.wikipedia.org/wiki/ISO_8601), that defines how dates are represented and referenced

Using that international standard, the "day of year number" is called the "ordinal date" (https://en.wikipedia.org/wiki/ISO_8601#Ordinal_dates).

Similarly, the standard defines the "week of year" number, which is just referred to as the "week number" (https://en.wikipedia.org/wiki/ISO_week_date).

There is also a standard for "weekday number" which is "a digit d from 1 through 7, beginning with Monday and ending with Sunday"

Note that the TWCore date formatting (https://tiddlywiki.com/#DateFormat) that is used by the <<now>> macro already supports "week number" using "WW" and "0WW" formatting codes.  It also supports both 4-digit and 2-digit "year number with respect to week number, using "wYYYY" and "wYY" formatting codes respectively.

-e

Ed Heil

unread,
Aug 28, 2020, 9:01:54 AM8/28/20
to TiddlyWiki
Hey, thanks all. 

That macro is outstanding.  I'd definitely rather exploit existing filter techniques than add in a plugin!

I had run across the term "ordinal date" in wikipedia for this thing, and "day of year" would also be an intuitively clear way to refer to it.  I'll pick one of those to use for my field. :)
Reply all
Reply to author
Forward
0 new messages