This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/

DateFormat

26th February 2023 at 2:43pm

The default representation of dates is a compact string such as 20211002153802059. The associated template is [UTC]YYYY0MM0DD0hh0mm0ss0XXX. For example, the created and modified fields are stored like this.

The display format for this string can be controlled with a template. For example, transcluding the modified field automatically applies a template to display the date as Sat Oct 02 2021 17:40:50 GMT+0200 (Central European Summer Time). A few widgets and filter operators allow you to manually specify a template, for example the ViewWidget:

<$view field=modified format=date template="DDth mmm YYYY 0hh:0mm:0ss" />

The date string is processed with the following substitutions:

TokenSubstituted Value
dddddIntroduced in v5.2.0 Day of year (1 to 365, or 366 for leap years)
0dddddIntroduced in v5.2.0 Zero padded day of year (001 to 365, or 366 for leap years)
DDDDay of week in full (eg, "Monday")
dddShort day of week (eg, "Mon")
ddddIntroduced in v5.2.0 Weekday number from 1 through 7, beginning with Monday and ending with Sunday
DDDay of month
0DDAdds a leading zero
DDthAdds a suffix
WWISO-8601 week number of year
0WWAdds a leading zero
MMMMonth in full (eg, "July")
mmmShort month (eg, "Jul")
MMMonth number
0MMAdds leading zero
YYYYFull year
YYTwo digit year
wYYYYFull year with respect to week number
aYYYYIntroduced in v5.1.23 Full year but negative dates are displayed as positive
wYYTwo digit year with respect to week number
{era:BCE||CE}Introduced in v5.1.23 Displays a different string for years that are negative, zero or positive (see below)
hhHours
0hhAdds a leading zero
hh12Hours in 12 hour clock
0hh12Hours in 12 hour clock with leading zero
mmMinutes
0mmMinutes with leading zero
ssSeconds
0ssSeconds with leading zero
XXXMilliseconds
0XXXMilliseconds with leading zero
am or pmLower case AM/PM indicator
AM or PMUpper case AM/PM indicator
TZDTimezone offset
TIMESTAMPIntroduced in v5.2.4 Number of milliseconds since the ECMAScript epoch, 1 January 1970.
\xUsed to escape a character that would otherwise have special meaning
[UTC]Time-shift the represented date to UTC. Must be at very start of format string

Note that other text is passed through unchanged, allowing commas, colons or other separators to be used.

The {era:BCE||CE} notation can specify different strings for years that are negative, zero or positive. For example {era:BC|Z|AD} would display BC for negative years, AD for positive years, and Z for year zero.

Examples

TemplateOutput
DDth MMM YYYY16th February 2011
DDth MMM \M\M\M YYYY16th February MMM 2011
DDth mmm YYYY 0hh:0mm:0ss16th Feb 2011 11:38:42

Using TIMESTAMP to calculate time difference

You can calculate the difference between two dates by doing the following:

  1. Convert both dates to timestamps
  2. Subtract the later date from the earlier one – if you don't know which one is earlier use the abs operator to get an absolute value after subtraction
  3. Divide the resulting number by the number of milliseconds in your chosen interval

Here is an example of calculating the number of days that passed between creation and last modification of current tiddler:

  • Convert the created and modified fields to timestamps
  • Divide their difference by 86400000 which is the number of milliseconds in a day
    • 1000 milliseconds per second × 60 seconds per minute × 60 minutes per hour × 24 hours per day = 86,400,000 milliseconds per day

<$let
	timestamp-modified={{{ [{!!modified}format:date[TIMESTAMP]] }}}
	timestamp-created={{{ [{!!created}format:date[TIMESTAMP]] }}}
	difference-days={{{ [<timestamp-modified>subtract<timestamp-created>divide[86400000]floor[]] }}}>

* ''Modified date:'' <$text text={{{ [{!!modified}format:date[YYYY-0MM-0DD]] }}}/>
* ''Created date:'' <$text text={{{ [{!!created}format:date[YYYY-0MM-0DD]] }}}/>
* ''Difference in days:'' <<difference-days>> days
</$let>