Localized Month Name?

How to modify this action Localized Day Name | Drafts Action Directory to return localized month names instead of days?

perhaps expand this action to return both localized day and month name?

I would take a different approach. That action requires editing to specify an array of potential values each time, but these should be known to the system in any case. Referencing those minimises the maintenance and risk of mis-keying entries.

Hereā€™s a few lines of JavaScript to illustrate the approach I would take.

const LOCALE_ID = "de-DE";
let dt = new Date();
alert(dt.toLocaleDateString(LOCALE_ID, { weekday: 'long' }));
alert(dt.toLocaleDateString(LOCALE_ID, { month: 'long' }));

The first line specifies what language/location we want to use. You can specify this using a standard identifier. Above Iā€™ve chosen Germany as the example.

  • Local Codes List - this list I think has the most commonly available locales listed.

The second line creates a new date object for the current date.

The third and fourth lines display a word via an alert box. They convert the current date to a day of the week, or a month, respectively; specified in the language associated with the locale.

In the example action it is creating a template tag, therefore the following example would generate such tags, but with the day of the week (tag = [[day]]) and the month (tag = [[month]]) being specified in Greek.

const LOCALE_ID = "el-GR";
let dt = new Date();
draft.setTemplateTag("day", dt.toLocaleDateString(LOCALE_ID, { weekday: 'long' }));
draft.setTemplateTag("month", dt.toLocaleDateString(LOCALE_ID, { month: 'long' }));

You can temporarily add a quick check line at the end too to ensure that it is working as expected. The following line will process the day and month tags and display the result in an alert.

alert(draft.processTemplate("[[day]] [[month]]"));

While this doesnā€™t modify the original action as per your request, I think it should be a better overall solution.

Hope that helps.

2 Likes

Fantastic! If @sylumer think he has a better approach, I believe him. Thank you again for your quick answer and helpful descriptions step-by-step guidance.

One little thing @sylumer, how to capitalise both day and month name? :blush:

I guess there are two ways to do it?

  • Define it on top in script itself
  • In the end just capitalise result

They should come through as capitalised (first character only in upper case) as they are proper nouns. If you mean all caps (upper case), then you can upper case any string with toUpperCase().

Amending the line to setup the month tag for example could be done like this.

draft.setTemplateTag("month", dt.toLocaleDateString(LOCALE_ID, { month: 'long' }).toUpperCase());

Dear @sylumer ā€¦ Dorry to say but Iā€™m a very innocent and fresh user of Drafts. Hence my questions might sound silly to you. But still I cannot get to work what Iā€™m actually looking for ā€¦

Iā€™m having a simply action like the following to quickly enter todayā€™s date

Insert Text
[[date|%d %B, %Y]]

How could I use your code suggested above to turning the ā€œmonth partā€ (%B) into my local DateString?

I already tried a couple of things but always ended in getting some error messages.

Thanks a lot in advance if you could help me there!

Start with the action being discussed in this topic. It is based on a Script step not an Insert Text step.

Dear @sylumer, thanks a lot for your speedy reply and for your availability to help!

I integrated your code as a Script step in my action. It worked well and gave me the two alert boxes including the correct ā€œweekdayā€ and ā€œmonthā€ names. So far so good.

But how to making use now of all that in an Insert Text step?

What I initially wanted to do is an ā€œInsert Text stepā€ that inserts the current date, but CORRECTLY LOCALIZED, and not according to the English nomenclature.

Iā€™m simply out of a clue how to making use of your Script step except of displaying those two alert boxes. Can I for instance use it to setting variables that I could then use in my Insert Text step?

Again SORRY for my stupid question. Iā€™m still trying to understand how things work ā€¦

In the original action (not mine). It says this:

Example script which can be incorporated into other actions to create tamplate tags with localized versions of day names.

Include this script in another action, and it will create a [[day]] template tag with the localized name of the day of the week which can be used it later action steps in the same action.

Edit the dayNames array to contain the names (or abbreviations) you would like returned for each day of the week, starting with Sunday.

Have you tried using the [[day]] tag as it indicates.


If you are still stuck after this, consider sharing the action(s) you have (via unlisted directory links), then people can help you debug what you have created.

Dear @sylumer, I finally got it to work! All working now as desired. THANKS a lot for helping me out and for being so patient! I know that it might be frustrating to answer those newbie questions over and over again. And therefore Iā€˜m particularly thankful that nevertheless you did.