Formatting date object in ECMAScript 262 Ed. 11

I have this item that is a DateTime and the state gives me a string inside my rule. I convert the string to a JS Date object with new Date and confirm the date to be an object with typeof.

Then I try to format this date from Thu Dec 14 2023 12:00:00 GMT+0100 (CET) to a more human readable format using toLocaleString() passing in both en-GB locale and various options. The output is though always the same as the original JS date object described here above.

I have also tried setting a simple string “2023-12-14 12:00:00” with new Date and it give me the same result.

I did read the javascript automation docs and the docs for js-joda and tried to use time. I did not fully understand how to implement js-joda DateTimeFormatter function.

Can anyone point me in the right direction so that I can format my dateTime from JS rule.

var options = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric'

var test = new Date("2023-12-14 12:00:00")
var sessionEnergy = parseFloat(items.getItem("EH85AGAZ__Easee_Brunkullas_Total_Session_Energy").state);
var sessionStart = new Date(items.getItem("EH85AGAZ__Easee_Brunkullas_Session_Start").state);
var sessionEnd = items.getItem("EH85AGAZ__Easee_Brunkullas_Session_End").state;

console.log(test.toLocaleString("en-GB", options));

var readableStartDate = sessionStart.toLocaleDateString()

var roundedSessionEnergy = sessionEnergy.toFixed(2);
var actions = actions.Things.getActions("pushover", "pushover:pushover-account:XXXXXXX")
actions.sendHtmlMessage('Laddat: <b>'+ roundedSessionEnergy +' kWh</b><br/>'+readableStartDate+'<br/>', 'Easee BKV10')

According to their docs js-joda doesn’t support toLocale so you’ll have to create a DateTimeFormatter and pass in the pattern. DateTimeFormatter | js-joda

Once you have the formatter you’ll call format.

Something like:

var sessionStart = time.toZDT(items.EH85AGAZ_Easee_Brunkullas_Session_Start);
var formatter = time.DateTimeFormatter.ofPattern('yyyy-MM-DD kk:mm:ss');
var readableStartDate = formatter.format(sessionStart);