Skip to main content
David One Zero
Participant
August 17, 2022
Answered

I can't seem to change to local date with Custom Javascript "toLocaleDateString"

  • August 17, 2022
  • 2 replies
  • 526 views

I'm constructing a form where I'm automating the days and adding extra days from the current day. All seems to be working great. The only issue I run into is that my InDesign is in English, and I would like to keep it that way, but the form is in Dutch. 

 

Now The current date and time works fine, i changed it to: 

dd MMMM yyyy HH:HH

which outputs as: 17 augustus 2022 11:11

 

No problem there. I also changed the Dictinoary to Dutch reform 2005. This does seem to change back to English UK Whenever I open and close the file. Might be the issue?

 

The .js i use is as follows:

#targetengine 'usernameVariable'

function addVariables(openEvent)  

{  

var doc = openEvent.parent;  

while ( doc.constructor.name != "Document" )  

  {  

if ( doc.constructor.name == "Application" ){ return; }  

    doc = doc.parent;  

  }  

// from http://stackoverflow.com/questions/563406/add-days-to-datetime

var today = new Date();  

var tomorrow = new Date(today);  

  tomorrow.setDate(today.getDate()+14);  
    
    const options = {
        year: 'numeric',
        month: 'long',
        day: 'numeric'
    };

  createTextVariable(doc, "Days+14", tomorrow.toLocaleDateString('nl-NL', options));  

}  

function createTextVariable(target, variableName, variableContents)  

{  

var usernameVariable = target.textVariables.itemByName(variableName);  

if (!usernameVariable.isValid)  

  {  

    usernameVariable = target.textVariables.add();  

    usernameVariable.variableType = VariableTypes.CUSTOM_TEXT_TYPE;  

    usernameVariable.name = variableName;  

  }  

  usernameVariable.variableOptions.contents = variableContents;  

}  

app.addEventListener('afterOpen', addVariables);  

which outputs as: Wednesday, August 31 2022

I would like it to output as: 31 augustus 2022 Hence:

    const options = {
        year: 'numeric',
        month: 'long',
        day: 'numeric'
    };

 

Hopefully some one can help me, thanks in advance.

This topic has been closed for replies.
Correct answer Marc Autret

Hi @David One Zero 

 

  • To my knowledge, Date.prototype.toLocaleDateString is hopelessly insensitive to $.locale in ExtendScript, at least when the target is InDesign. Instead, it honores the app.locale property, which is read-only 😞 (That's indeed very annoying.)

 

  • Furthermore, ExtendScript does not implement the locales and options arguments of toLocaleDateString (they were introduced in a more recent version of JS.)

 

Thus, I'm afraid you have to implement your own Date formatter. That said, in your particular example the routine seems quite simple—as you only need localized month names. Basically, the solution should look like this:

 

https://gist.github.com/indiscripts/fab9f65ef274fbb35d6b8ed192074283

 

Best,

Marc

2 replies

Marc Autret
Marc AutretCorrect answer
Legend
August 19, 2022

Hi @David One Zero 

 

  • To my knowledge, Date.prototype.toLocaleDateString is hopelessly insensitive to $.locale in ExtendScript, at least when the target is InDesign. Instead, it honores the app.locale property, which is read-only 😞 (That's indeed very annoying.)

 

  • Furthermore, ExtendScript does not implement the locales and options arguments of toLocaleDateString (they were introduced in a more recent version of JS.)

 

Thus, I'm afraid you have to implement your own Date formatter. That said, in your particular example the routine seems quite simple—as you only need localized month names. Basically, the solution should look like this:

 

https://gist.github.com/indiscripts/fab9f65ef274fbb35d6b8ed192074283

 

Best,

Marc

David One Zero
Participant
August 19, 2022

Hi Marc,

 

This is exactly what i was looking for. You are a saint!

I didn't for a moment think of the fact that Adobe uses ExtendScript (prior to this i didn't know).

 

Thanks again for your help, you made my week.

 

 

Peter Kahrel
Community Expert
Community Expert
August 17, 2022

Your locale doesn't change by setting a dictionary in InDesign. You probably need to set the scripting locale:

$.locale = 'nl_NL';

P.

David One Zero
Participant
August 18, 2022

Hi Peter,

 

Thank you for taking time to read and respond to my problem! 

I tried your suggestion, unfortunately this doesn't change anything.