Modifying date-time with timezone

I'm trying to parse 3 strings (start_day, start_time, duration) that come from my backend response into 2 datetime strings (start_dateTime, end_dateTime).

With a custom JS policy I tough I could do something like this: 

 

 

//Example data gotten from response
var timezone = "+02:00";
var start_date = "2022-06-01"; 
var start_time = "13:00";
var duration_in_mins = "60";

function formatDate(date) {
  return (
    date.toLocaleString("sv-SE", { timeZone: "Europe/Rome" }) + timezone
  ).replace(" ", "T");
}
var startdateTime = new Date(startDate+"T" + startTime + timezone);
var endDateTime = new Date(startdateTime.getTime() + duration_in_mins * 60 * 1000);

context.setVariable("order.StartDateTime", formatDate(startdateTime));
context.setVariable("order.EndDateTime", formatDate(endDateTime));

 

 

 And it works when I do something similar in a pure JS sandbox

But it seems that the Apigee env ignores the date.toLocaleString method. It even returns in a en-US format as such 

 

 

//Expected

StartDateTime : "2022-06-02T17:00:00+02:00",
EndDateTime : "2022-06-02T18:00:00+02:00"

//Actual

"StartDateTime": "JuneT21, 2022 1:00:00 PM GMT+02:00",
"EndDateTime": "JuneT21, 2022 5:00:00 PM GMT+02:00",

 

 

There seems to be more than a few Issues. First the formatting is wrong, and the difference is completly wrong.

 

What is happening here? Why is the LocalDate being ignored? Why does it seem the times are completly wrong? What can I do to fix it? 

0 5 405
5 REPLIES 5

The incorrectly implemented .toLocaleString() is an open issue in Rhino.  The JS callout in Apigee depends on Rhino. So you're seeing broken behavior because of that underlying Rhino issue. 

As for a workaround, I can suggest

  • write your logic in Java
  • OR, don't take a dependency on .toLocaleString(). Implement it yourself somehow.  Maybe use UTC and convert to your Locale manually.

Probably neither of those is particularly attractive. I don't see a better alternative. 

You could look for an older JS library that parses and formats Date strings to fit your needs. You'd need to "browserify" any library in order to be able to use it within the JS callout.  Most of the libraries that do this sort of thing are probably older and not maintained since much of the needed capability is in the mainstream ES language now. I don't have a specific library to suggest here.

If I were doing this , I'd use a Java callout. See attached for an example.

 

did it work?

Sorry for the very late reply.

What we ended up finding was 2 different solutions, the first one was as you mentioned, use a different programming language, in our case Java. The second one was including the source code for the moments.js library in our Apigee and use that library.

 

nice. thanks for the reply.

Hi, I am also trying to parse 3 strings (start_day, start_time, duration) that come from my backend response into 2 datetime strings (start_dateTime, end_dateTime) and I was facing the same issue. But thanks to you for the suggestion. It helped me too. When I was searching for it online, I also found https://www.topessaywriting.org/samples/manufacturing website link where I found a lot of essay samples and the best thing is I can read them for free.