Increment 1 day to a field

HI ,

with the help of forum members ,i am now using the below code to increment “1” to a custom field using a button

my question is . what if i want to increment a day , to an existing date , is this achievable ?
i tried different things and realized that a static value wouldn’t work, as not all month are the same number of days …

and so is there some prexisiting function in TW that can manage this calculation .

also on a side note , is there also something that would do the calculations skipping week ends, so something ala excels networkdays formula ?

Thank you

\define increment(field:endd)
<$action-setfield $field=$field$ $value={{{ [all[current]get[$field$]!is[blank]else[0]add[24000000]] }}}/>
\end


<$button actions=<<increment endd>>>
+1
</$button>
1 Like

Hi,

You should search for ParseDate (another brilliant tool by @EricShulman) in this forum.
One solution to add days to a date is to convert it to Unix date format beforehand, then add the right amount of seconds, and convert the result back to the original format.

Fred

1 Like

Here’s the link:
https://tiddlytools.com/#TiddlyTools%2FTime%2FParseDate

Also, note that “Unixtime” is specified in milliseconds. Thus, you need to multiply “the right amount of seconds” by 1000 when adding to the Unixtime value before converting back to formatted date/time text.

enjoy,
-e

1 Like

Thanks all ,

i used the parser just as fred sugested, changed to unix added the day in unix then converted back to normal format

thanks again for all your help :slight_smile:

Edit : i actually forgot the second part of my question , is there something that can calculate days skipping week ends , so only week days ?

Try this:

<$let
  before={{{[[20230317000000000]]}}}
  timestamp={{{[<before>format:date[TIMESTAMP]]}}}
  oneday={{{[[1000]multiply[60]multiply[60]multiply[24]]}}}
  after={{{[range[1],[3]multiply<oneday>add<timestamp>unixtime[YYYY0MM0DD0hh0mm0ss0XXX]] :filter[<currentTiddler>format:date[dddd]compare:integer:lt[6]] +[first[]] }}}
>

;Before
:<<before>>
;After
:<<after>>
</$let>

What it does:

  • define variable before, for example with next wednesday’s date
  • define variable timestamp as before converted to Unix date
  • define variable oneday as 1 day in milliseconds
  • for after, it’s a little bit tricky: first compute values of 1, 2 and 3 days in milliseconds, then add timestamp to each value and convert back to TiddlyWiki’s native date format. Then filter these 3 values: get their “day of week number”, and keep only those before saturday. Lastly, remove every remaining value but the first one.

You can test this code by changing the 17 part of before definition.

Fred

2 Likes

Although difficult to use this sounds like the days operator would be easier.

@TW_Tones,

The days[] operator requires input in the form of a TWCore 17-digit date that is stored in a specified tiddler field (e.g., modified), while @tw-FRed’s solution uses date values that are held in variables without using any tiddlers.

1 Like

@tw-FRed… nicely done!

However, there is one small bug in your implementation: when using the unixtime[...format...] filter operator, the format needs to convert the timestamp to a [UTC] date output in order to avoid applying an implicit adjustment for local timezone offset.

There are two ways to achieve this:

A) Use a variable to add the [UTC] prefix to the unixtime filter operand, like this:

<$let
  before="20230317000000000"
  timestamp={{{[<before>format:date[TIMESTAMP]]}}}
  oneday={{{[[1000]multiply[60]multiply[60]multiply[24]]}}}
  format="[UTC]YYYY0MM0DD0hh0mm0ss0XXX"
  after={{{[range[1],[3]multiply<oneday>add<timestamp>unixtime<format>] :filter[<currentTiddler>format:date[dddd]compare:integer:lt[6]] +[first[]] }}}
>

note: the format variable is needed because you can’t use a value containing square brackets as as a literal filter operand

OR

B) use the parsedate:unixtime[] filter operator (with no format operand), which applies a default output format of [UTC]YYYY0MM0DD0hh0mm0ss0XXX without needing to specify a filter operand at all, like this:

<$let
  before="20230317000000000"
  timestamp={{{[<before>format:date[TIMESTAMP]]}}}
  oneday={{{[[1000]multiply[60]multiply[60]multiply[24]]}}}
  after={{{[range[1],[3]multiply<oneday>add<timestamp>parsedate:unixtime[]] :filter[<currentTiddler>format:date[dddd]compare:integer:lt[6]] +[first[]] }}}
>

ref: for the unixtime[...] or parsedate:unixtime[] filter operators, you will need to install TiddlyTools/Time/ParseDate. Note that there is an outstanding GitHub ticket to incorporate the parsedate:unixtime[] filter operator into the TWCore as a new variety of the existing format operator (see [IDEA] add `format:timestamp[dateformat]` filter operator · Issue #7120 · Jermolene/TiddlyWiki5 · GitHub)

enjoy,
-e

1 Like

The corresponding PR should be about done, but I’m not sure if this will make it into 5.2.6. At this time, only bug-fix-PRs are merged.

Hi,

Thank you very much for your answers , it was a long shot honestly , wasnt expecting a solution ,

i learn every day how powerful TW is :slight_smile:

thanks again

Hi,

sorry for coming back to this again , but i have a new requirement, i need to increment more than “1” , the script seems to handle 1 or 2 increments but not more, before starting to count weekends back again .

thank you