Javascript: help with a snippet for tagging due and overdue tasks

Hi, all—

I have a script that I run as a little sanity check each morning. Among other things, it looks at due dates and should tag anything with a due date that matches today as @today and anything with a due date that’s expired as @overdue.

Here’s the script for that specific task:

Node.commonAncestorsForNodes(tree.evaluateNodePath('//@due')).forEachNodeInSet(function(node) { var now = moment().format('YYYY-MM-DD'); var dueCheck = new Date(node.tag('due')); if (moment(dueCheck).isSame(now)) { node.addTag('today'); } if (moment(dueCheck).isBefore(now)) { node.addTag('overdue'); } })

This catches any overdue tasks, but for some reason, it doesn’t catch anything with a due date that matches today. Can anyone see what I’m doing wrong?

Thanks in advance for any assistance.

I haven’t stepped through this to check, but at first glance it looks as if the issue is that a dateTime value always has a time component, even if that is midnight.

dueCheck looks likely to have the time of its creation.

Generally what you need to check for is not the equivalence of two dates (rare unless they are identical down to thousands of a second), but rather: whether one date falls between two other limits e.g. start of business and end of business on a particular day, or midnight last night, and a millisecond before midnight tonight.


TimeZone is a further addition to the need to always look at a triangle of dateTimes (is A between B and C, rather than does A=B) there is also the nuance that not all midnights are the same – one dateTime may contain 00:00:00 and the other 00:23:00 +1h GMT

Ahhhhhhh… Lightbulb. :))