Help with date format

Hi all I’m hoping someone can help me with a problem. I need to write the date as for example 02/iv/2021. It would be fantastic if I could use an action to do this. Can anyone help please? Thank you :blush:

We need to know a little more. Do you want to insert the current date in this format, or do you want to convert an arbitrary date to this format? If the latter, In what form is the date you want to convert? Is it a string or a set of three numbers?

Yes sorry insert the current date in this format.

Try an action with a Script step of

let romanMonths = ['i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii', 'ix', 'x', 'xi', 'xii']
let today = new Date();
let day = today.getDate().toString().padStart(2, '0');
let month = romanMonths[today.getMonth()];
let year = today.getFullYear().toString();
let rDate = [day, month, year].join('/');

draft.setTemplateTag('romanDate', rDate);

followed by an Insert Text action with the [[romanDate]] tag.

Is that a Roman numeral? In a date? I’m not sure I want to know the story behind this requirement. That said, I’m pretty sure strftime does not support directly, so it would probably require a script. If it’s only the month you need to convert, that’s not too bad. I might keep it simple and do a replacement table, like:

const months = {
  "01": "i",
  "02": "ii",
  "03": "iii",
  "04": "iv",
  "05": "v",
  "06": "vi",
  "07": "vii",
  "08": "viii",
  "09": "ix",
  "10": "x",
  "11": "xi",
  "12": "xii"
}
// get date like 02/03/2021
let d = draft.processTemplate("[[date|%d/%m/%Y]]");
// replace month
for (let m in months) {
	d = d.replace(`/${m}/`, `/${months[m]}/`);
}

let [st,len] = editor.getSelectedRange();
editor.setSelectedText(d);
editor.setSelectedRange(st+d.length, 0);

Looks like the good Dr. beat to me to it, but either of these would work.

1 Like

Looks like both @drdrang and @agiletortoise beat me to it! I was working on a solution in parallel. Here’s what I came up with:

let d = new Date()
let numerals = ["i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", "xi", "xii"]
let str = strftime(d, `%m/${numerals[d.getMonth()]}/%Y`)
editor.setSelectedText(str)

Always fun to compare different approaches.

Oh thank you that’s awesome

Lol it’s an entomology thing so every country can understand. Thanks very much for your help

Yes, I was trying to find out whether strftime supported Roman Numerals, and I read some suggestions that %Om would work, but it didn’t seem to as far as I could tell.

As for the strange format. My guess is that @Simon_Robson is British perhaps? I recognise this format as a slightly old-fashioned British way to write dates. I’ve occasionally received handwritten letters using the format 2.iv.21, for example.

1 Like

I didn’t know JavaScript had a strftime function. That’s much simpler.

Hi even worse a British entomologist :joy:

3 Likes

I don’t think it’s built into the language. I believe @agiletortoise added it as a helper function to the Drafts scripting environment.

Yes, it’s a Drafts addition, just a wrapper for the OS’s strftime.

FWIW, and apparently adding to the plethora of solutions that have emerged:

I’d spotted a tweet early this morning about doing this from @Simon_Robson, and since I was doing an update to my action group suite today, I bundled some things in. There’s a function for converting integers to Roman numerals, and I used that to build an action (TAD-Insert dd/{mm}/yy) to insert the current date in the specified format.

The action is available in the Writing action group.


After including the TAD action to load in the custom functions from the library, the action uses the following script to insert the date and place the cursor after it.

strDate = draft.processTemplate("[[Date|%d]]") + "/" + Math.TA_integerToRoman(draft.processTemplate("[[Date|%m]]"), false) + "/" + draft.processTemplate("[[Date|%y]]");
editor.TA_insertTextPosAtEnd(strDate);
2 Likes

Fantastic thank you everyone