Action: “Add To Log”
#Tomorrow comes too soon
My Problem
Sometime between 9:30 pm and 11 pm the script decides it’s tomorrow and creates a new day’s log. So tonight’s last entries go into the wrong day, but the time is correct.
Action Directory listing
Posted by @tlmnlpstz, Last update almost 5 years ago
Creates a new draft or appends to an existing draft with the title “# YEAR-MM-DD Log” (can be changed in the script). The Log draft is tagged with “journal” (can be changed in the script).
New lines are created from the [[draft]] with this pattern: HH:MM: [[draft]]
## The script
// tag to assign to drafts
const logTag = "log";
// create date variable
var dte = new Date();
var date = dte.toISOString().slice(0,10);
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(11, 16);
// grab text
const currentContent = "**" + localISOTime + ":** \n" + draft.content.trim();
// query for drafts...
var drafts = Draft.query(date, "inbox", [logTag]);
// loop over found drafts looking for a matching list
var d;
for (var draft of drafts) {
if (draft.content.startsWith("# " + date + " Log")) {
d = draft;
}
}
// if we didn't find the list, create it...
if (!d) {
d = Draft.create();
d.content = "# " + date + " Log" + "\n";
}
// tag and update content
d.addTag(logTag);
d.content = d.content + "\n" + currentContent + "\n"
d.update();