Dates - Adding and Formatting

70 views
Skip to first unread message

Stobot

unread,
Apr 24, 2020, 6:53:56 AM4/24/20
to TiddlyWiki
Hello all,

I'm often building out calendar lists and so using variants of date plugins from others - mostly add-time from Jed / inmysocks. I find that on a case by case basis I need the date in a different format. Sometimes the "native" YYYY0MM0DD, usually ISO YYYY-0MM-0DD, sometimes just the MMDD part (my current need) etc. Ideally the <$view format="date" template="..."> widget would allow me to fed it a macro as input, but it appears to only pull from a tiddler field.

Currently I'm just copy-pasting the add-time.js over and over with different names for each format I need, and struggling through modifying the javascript (I'm not a programmer) each time, but thought somebody could either tell me that there's an easy way to feed a value from a macro into <$view> widget without writing it to a tiddler first, or have an enhanced version of add-time.js that allowed a "template" string parameter to format the output (I would consider this ideal). I've been trying to understand javascript date formatting hoping that natively I could pass it something like "YYYY-MM-DD" but that doesn't appear to be the case.

Any ideas?

Saq Imtiaz

unread,
Apr 24, 2020, 7:08:31 AM4/24/20
to TiddlyWiki
As far as I can tell, the template parameter in the ViewWidget expects a string. So you should be able to do:
<$view format="date" template="YYYY-MM-DD"/>

Or

<$view format="date" template=<<mymacro>> />

Stobot

unread,
Apr 24, 2020, 7:41:36 AM4/24/20
to TiddlyWiki
Sorry if I was unclear Saq. The macro doesn't generate the format, it generates the date. I need to pass <$view> a date *other than today* and have it apply the formatting.

I should point out in case anyone else finds it helpful, while Jed's add-time.js (http://inmysocks.tiddlyspot.com/#%24%3A%2Finmysocks%2Fmacros%2Fadd-time.js) is great, it didn't let me start with any other date than today, which I was able to tweak. This code (below) is what I'd love to add a parameter string to so that I can also pass it "YYYY-MM-DD". I hope that makes more sense.

/*\
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"}
];

/*
Run the macro
*/

exports
.run = function(basedate, days, months, years) {
 
 
//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());

 
return result;
};

})();

Saq Imtiaz

unread,
Apr 24, 2020, 8:49:02 AM4/24/20
to TiddlyWiki
aha. OK. Try this but backup your wiki first, this is untested:

/*\

THIS IS A MODIFIED COPY

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); 
 }
};

})();

If this works, suggest you rename it (both in the comment at the top and the tiddler title) to avoid confusion in the future.

TonyM

unread,
Apr 24, 2020, 8:52:18 AM4/24/20
to TiddlyWiki
If I have this correct you want the same as me. Ie allow the input to the view eidger to come from a variable rather than just a tiddler or field?

Evans formulae plugin has tgath ability I believe.

Regards
Tony

Stobot

unread,
Apr 24, 2020, 10:32:14 AM4/24/20
to TiddlyWiki
Saq - that's perfect! Exactly what I wanted. Had no idea about $tw.utils.formatDateString. Thanks so much!

TonyM - good point about the formula plugin, that'd be an option as well.
Reply all
Reply to author
Forward
0 new messages