Incorrect Format for JavaScript Date String
search cancel

Incorrect Format for JavaScript Date String

book

Article ID: 278165

calendar_today

Updated On:

Products

CA Process Automation Base

Issue/Introduction

When running the following JavaScript code inside a "Run Script" operator, we are not seeing the expected result:

//------------- Begin Script -----------------

currentDateDE = new Date();
newDateDE = new Date(currentDateDE);
newDateDE.setDate(currentDateDE.getDate() - 30);

day_de = newDateDE.getDate();
year_de = newDateDE.getFullYear();

months_DE = [
  "Januar", "Februar", "März", "April", "Mai", "Juni",
  "Juli", "August", "September", "Oktober", "November", "Dezember"
];

month_de = months_DE[newDateDE.getMonth()-8];
formattedDate_de = month_de + " " + day_de + ", " + year_de;

create_date_de1 = formattedDate_de;
create_date_de2 = (new Date()).toLocaleDateString('de-DE'); 

//------------- End Script -----------------

In the above example,

  • The value of "create_date_de1" correctly displays the long date format with the month's German name.  For example, "Dezember 19, 2023."
  • The value of "create_date_de2" incorrectly displays the long date format with the month's English name.  For example, "January 18, 2024."

Environment

CA Process Automation 4.3.x and up

Cause

ITPAM runs JavaScript code inside of processes and workflows using Rhino, a popular JavaScript engine.  JavaScript can behave differently when running from a JavaScript engine, as compared with the results from the vanilla JavaScript.  In this scenario, the Rhino behavior for a few APIs varies from the vanilla JavaScript.  In particular, Rhino ignores the parameters passed to the toLocaleDateString method; hence, toLocaleDateString('de-DE') is giving results in English only.

Resolution

One way to format a long date when using a locale other than English is to follow the example of create_date_de1 above and build the date string as follows:

currentDateDE = new Date();
newDateDE = new Date(currentDateDE);
newDateDE.setDate(currentDateDE.getDate() - 30);
day_de = newDateDE.getDate();
year_de = newDateDE.getFullYear();

months_DE = [
 "Januar", "Februar", "März", "April", "Mai", "Juni",
 "Juli", "August", "September", "Oktober", "November", "Dezember"
];

month_de = months_DE[newDateDE.getMonth()-8];
formattedDate_de = month_de + " " + day_de + ", " + year_de;

Another way is to create a "simpleDateFormat" object that enforces the locale you need:

//START:get the locale date using java
var Locale = Packages.java.util.Locale;
var locale = new Locale("de", "DE");
var SimpleDateFormat = Packages.java.text.SimpleDateFormat;
var simpleDateFormat = new SimpleDateFormat("MMMM d, yyyy", locale);

//------------- GERMAN -----------------
currentDateDE = new Date();
newDateDE = new Date(currentDateDE);
newDateDE.setDate(currentDateDE.getDate() - 30);

day_de = newDateDE.getDate();
year_de = newDateDE.getFullYear();

months_DE = [
  "Januar", "Februar", "März", "April", "Mai", "Juni",
  "Juli", "August", "September", "Oktober", "November", "Dezember"
];

month_de = months_DE[newDateDE.getMonth()];
formattedDate_de = month_de + " " + day_de + ", " + year_de;

create_date_de1 = formattedDate_de;
create_date_de2 = (new Date()).toLocaleDateString('de-DE'); // deutscher Monatsnamen

Process.Ger_Date_1 = "'" + create_date_de1 + "'";
Process.Ger_Date_2 = "'" + simpleDateFormat.format(new Date()) + "'";