days[] filteroperator does not work when the argument is 0.

37 views
Skip to first unread message

Arkady Grudzinsky

unread,
Mar 17, 2016, 1:03:15 PM3/17/16
to TiddlyWiki
I am trying to use the days[] filter operator to determine whether my custom "scheduled" date field is in the past.  I have found that the days[] operator works inconsistently with and without an argument.
  • When days interval is positive,days[+N] returns "FALSE" when the date field is more than N days in the future and "TRUE" when the date field is within N days from today in the future or in the past.
  • When days interval is negative, days[-N] returns "FALSE" when the date field is more than N days in the past and "TRUE" when the date field is within N days from today in the past or in the future.
  • But when the days interval is 0, days[] returns "TRUE" only when the date field is set to TODAY.  It returns "FALSE" for both future and past dates.  This works to determine whether the date field is within 0 days from today in the past, but this is incorrect for the future dates.
I'm still not sure if it's a bug or a feature.  So, the days[] filter can be used whether a date field is past N days in the future or N days in the past, but it cannot be used to determine whether a date is past TODAY because it returns FALSE for both future and past dates when days interval is 0.

I suppose this behavior went unnoticed because the filter is mostly used with "modified" and "created" fields which are always in the past.

I could not understand where the inconsistency in days.js comes from.  Perhaps, when "sigh" is 0, it is unclear whether it is 0 days in the future or 0 days in the past.

To solve my problem, I created my own filter "passed.js" by stripping down "days.js".  "passed.js" simply returns "TRUE" when the date field is in the past and "FALSE" when it is in the future.  I do not use setHours(0,0,0,0) there, so it takes time into account as well.  It might be a useful filter to add to TW5 permanently.

/*\
  title: $:/core/modules/filters/passed.js
  type: application/javascript
  module-type: filteroperator

  Filter operator that selects tiddlers with a specified date field in the past.

  \*/
(function(){

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

    /*
      Export our filter function
    */
    exports.passed = function(source,operator,options) {
var results = [],
   fieldName = operator.suffix || "modified",
   isInThePast = function(dateField) {
return new Date(dateField) < new Date();
   };

if(operator.prefix === "!") {
   source(function(tiddler,title) {
if(tiddler && tiddler.fields[fieldName]) {
   if(!isInThePast($tw.utils.parseDate(tiddler.fields[fieldName]))) {
results.push(title);
   }
}
   });
} else {
   source(function(tiddler,title) {
if(tiddler && tiddler.fields[fieldName]) {
   if(isInThePast($tw.utils.parseDate(tiddler.fields[fieldName]))) {
results.push(title);
   }
}
   });
}
return results;
    };

})();



Reply all
Reply to author
Forward
0 new messages