I'm similarly surprised that there's not an easier way to do this with 
core. I'm not really a Javascript user generally, but I took Jed's useful 
add-time.js and tweaked slightly to base it off of a base date, rather than 
current date. I worry about plugins also because of making my wiki 
"heavier" or future compatibility concerns, but this is super light-weight 
(code is short enough I'll post below) and I don't think reliant on any 
core things worth noting - so future compatibility shouldn't be an issue. 
My general rule is to try to only use plugins I can understand / maintain 
if necessary, so they have to be pretty dang simple. When I modified Jed's 
code slightly, I have this which I add to all my wikis:

If you feel like using this, just start a new tiddler, name it something 
like $:/macros/dateadd.js, change the tiddler type to 
"application/javascript" and save/reload. Then, you can do stuff like:
<<dateadd basedate:"20201201" days:20">> to get 20201221. I found it useful 
to continue to use YYYYMMDD notation because all of the stock dateformats 
start that way, and the days filter operators work with that format. 

/*\
title: $:/stobot/macros/dateadd.js
type: application/javascript
module-type: macro

Takes a base date and adds days, months or years

\*/
(function(){

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

/*
Information about this macro
*/

exports.name = "dateadd";

exports.params = [
    {name: "basedate"},
    {name: "days"},
    {name: "months"},
    {name: "years"},
    {name: "template"}
];

/*
Run the macro
*/
exports.run = function(basedate, days, months, years, template) {
    
    //Make each date object.
    
    if (basedate === "") {
        var newdate = new Date();
    } else {
        var baseyear = basedate.substr(0,4);
        var basemonth = basedate.substr(4,2);
        var baseday = basedate.substr(6,2);
        var newdate = new Date(Number(baseyear), Number(basemonth)-1, 
Number(baseday), 0, 0, 0);
    }

    var new_year = Number(newdate.getFullYear())+Number(years);
    var new_month = Number(newdate.getMonth())+Number(months);
    var new_day = Number(newdate.getDate())+Number(days);

    var output_date = new Date(new_year, new_month, new_day, 0, 0, 0);

    var result = (output_date.getFullYear()*10000) + 
((output_date.getMonth()+1)*100) + (output_date.getDate());

    if(template === ""){
        return result;
    } else {
        return $tw.utils.formatDateString(output_date,template);
    }
};

})();

On Wednesday, December 30, 2020 at 6:50:30 PM UTC-5 TW Tones wrote:

> Soren,
>
> If you are only using it for short period such as 7-30 days, it would be 
> possible to simple handle "Decembers" differently to fix the year problem. 
> Or make use of the days operator.
>
> Other date tools exist, perhaps review Eric's powerful 
> http://tiddlytools.com/timer.html or Evans formulae plugin.
>
> Tones
>
> On Thursday, 31 December 2020 at 10:37:59 UTC+11 Soren Bjornstad wrote:
>
>> Tones,
>>
>> Thanks! I haven't gotten around to upgrading to 5.1.23 yet, so I'll give 
>> it a shot when I do. If it is unable to update the year, though, I don't 
>> think it will work in this case as I'm definitely going to need to cross 
>> year boundaries.
>>
>> On Tuesday, December 29, 2020 at 9:01:38 PM UTC-6 TW Tones wrote:
>>
>>> Soren,
>>>
>>> There is a new format operator for dates;
>>>
>>> Past this in a tiddler on tiddlywiki.com and see the result.
>>>
>>> Note how it seems to honor months and leap years but will not increment 
>>> the year.
>>>
>>> {{{ [all[current]get[created]format:date[YYYY0MM0DD]] }}}
>>> {{{ 
>>> [all[current]get[created]format:date[YYYY0MM0DD]add[1]format:date[YYYY0MM0DD]]
>>>  
>>> }}}
>>> {{{ 
>>> [all[current]get[created]format:date[YYYY0MM0DD]add[2]format:date[YYYY0MM0DD]]
>>>  
>>> }}}
>>>
>>> {{{ [[20000228]format:date[YYYY0MM0DD]add[1]format:date[YYYY0MM0DD]] }}}
>>> {{{ [[20000228]format:date[YYYY0MM0DD]add[2]format:date[YYYY0MM0DD]] }}}
>>>
>>> {{{ [[20210228]format:date[YYYY0MM0DD]add[1]format:date[YYYY0MM0DD]] }}}
>>> {{{ [[20210228]format:date[YYYY0MM0DD]add[2]format:date[YYYY0MM0DD]] }}}
>>>
>>> Regards
>>> Tones
>>>
>>> On Wednesday, 30 December 2020 at 10:31:50 UTC+11 Soren Bjornstad wrote:
>>>
>>>> Hi all,
>>>>
>>>> I'm trying to use Mark S.'s solution, and it works great, except it 
>>>> appears to have a minor bug in leap years: when the result would be the 
>>>> 29th of February, it instead returns the 28th of February. It otherwise is 
>>>> doing all of the calculations for leap years correctly (e.g., adding 2 
>>>> days 
>>>> on February 28, 2020 returns March 1, 2020).
>>>>
>>>> I have to admit I have pretty much no clue what's going on with those 
>>>> filters, lol, so if someone is able to spot the issue I'd be much obliged.
>>>>
>>>> On Tuesday, October 22, 2019 at 3:00:28 AM UTC-5 Mat wrote:
>>>>
>>>>> Cool stuff Mark!
>>>>>
>>>>> Date manipulation is one of those border areas that you seemingly will 
>>>>> never need... until you do. So thanks for sharing Mark.
>>>>>
>>>>> <:-)
>>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/c50afd28-63fa-4d4f-9586-adb316bd0154n%40googlegroups.com.

Reply via email to