Tomorrow comes too soon

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

It seems to be the way that script was written and intentional, but it’s creating the log values and resulting files based on ISO time, which is always in UTC - so Greenwich Mean Time. I assume you are probably in the states, so midnight in the UK is not what you want here.

It’s a bit of an odd script, because they also convert the time back to a local version. I’d suggest changing it to just use Drafts’ own template tags to generate date, which will be in the local timezone by default.

Something like the changes suggested below…

// BEGIN REMOVE LINES - replace this block 
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);
// END REMOVE LINES

// BEGIN ADD LINES - replace above block with ...
let date = draft.processTemplate("[[date|%Y-%m-%d]]")
let localISOTime = draft.processTemplate("[[date|%H:%M]]")
// END ADD LINES
1 Like

Sorry, had to comment to let you know that your post title sounds like a movie title :upside_down_face: