Help with action script (org-schedule)

Hello everyone! first time posting

I’ve been trying to get into org-mode and found an action called org-schedule to insert a date in org-mode format.

The problem I’m having is after picking a date, part of the inserted date is one day in the future the other is ok.

Can someone help me looking at this script and telling me what to change so the date inserted matches the date picked?

Org-Schedule

// Org-mode insert scheduled or deadline date & time

var p = Prompt.create();
var aScheduleTypes = [‘SCHEDULED:’, ‘DEADLINE:’];

p.title = ‘Scheduled or deadline?’;
p.isCancellable = true;

p.addSelect(‘scheduleSelect’, ‘select type:’, aScheduleTypes, [‘SCHEDULED:’], false);
p.addDatePicker(‘scheduled’, ‘select date:’, new Date(), {‘mode’: ‘dateAndTime’});
p.addButton(‘Ok’);

var result = p.show();

if (result) {

var dateString = new Date(p.fieldValues['scheduled']);
// format date/time for org-mode
var orgDate = dateString.toISOString().slice(0, 10);
var orgTime = dateString.toLocaleTimeString('en-US', {hour12: false});
var orgDay = dateString.toLocaleDateString('en-US', {weekday: 'short'});
var scheduleType = p.fieldValues['scheduleSelect'];
var orgScheduled = scheduleType + ' <' + orgDate + ' ' + orgDay + ' ' + orgTime + '>\n';
var orgScheduledLength = orgScheduled.length - 1;
// get insert position & draft content
var selRange = [draft.selectionStart, draft.selectionLength];
var text = draft.content;
// insert scheduled tag
draft.content = text.substring(0, selRange[0]) + orgScheduled + text.substring(selRange[0] + selRange[0]);
var selEditorRange = editor.getSelectedRange();
alert(orgScheduled);
editor.activate();
editor.setSelectedRange(selEditorRange[0] + orgScheduledLength, 0);

}

After a bit of searching I found an action group containing the action you are using. Note the action name has a “d” on the end.

I searched for this as the code you posted is mis-formatted. The forum software messes with quotes if you just post code. You have to wrap them in triple back ticks to make it a code block to stop that from happening; but often the easiest thing to do is just post a link to the installation page in the directory.

I have tried running the action and regardless of the options I select, I continue to get a date and time that is correct. I tried this as I couldn’t see any obvious cause in the code at first look.

Could you provide a worked example (what you enter) and highlight the inaccuracy in the output? If we have an error case, it should be easier to focus in on the problem code.

1 Like

You are calling .toISOString() on the date, which returns the time in UTC, so your day date value might be different than your current date, depending on what time zone you are in.

Not sure, but it looks like the date format you are looking for could be done with strftime, like:

let d = new Date(p.fieldValues['scheduled']);
let dateString = strftime(d, "%Y-%m-%d %a %H:%M:%S");
1 Like

You’re right, next time I’ll be more careful with the name, it ended with a “d” as you said.

It seems I must have messed with the code when trying to take out the time. I tried downloading the action again (which I couldn’t find before) and it’s working as intended now.

Thank you!