Can't get .toLocaleDateString('en-GB') to work keeps it as US

I am using the following code to return a GB date and time format but it just won’t change it from a US format
What am i doing wrong.
const sd = new Date()
const SysDate = sd.toLocaleDateString(‘en-GB’)
const Systime = sd.toLocaleTimeString(‘en-GB’, {timeStyle: ‘short’ } )

Your code you posted should work.The problem is probably somewhere else.

Sorry I should have said this is backend code.

If i run it as on page code its fine, if i call it as a function from backend it’s wrong.

I guess it must be the way Wix code works in backend or doesn’t more to the point.

Anyone have a work around?

Can you post your code (including the backend code and the call to backend)?

@jonatandor35 So Backend code is :
export function Datestuff(){
const sd = new Date()
const SysDate = sd.toLocaleDateString(‘en-GB’)
const Systime = sd.toLocaleTimeString(‘en-GB’, {timeStyle: ‘short’ } )
const SysHours = “” + sd.getHours()
const SysMinutes = “” + sd.getMinutes()
console.log(SysDate)
}

Called via:
export function button1_click(event) {
Datestuff()
}

Plain front end stuff is/was:
export function button1_click(event) {

const sd = new Date()
const SysDate = sd.toLocaleDateString(‘en-GB’)
const Systime = sd.toLocaleTimeString(‘en-GB’, {timeStyle: ‘short’ } )
const SysHours = “” + sd.getHours()
const SysMinutes = “” + sd.getMinutes()
console.log(SysDate)
}

@tom66859 it’s not enough for locating the issue. I assume you import from a jsw file at the top or your code? Right?
Can you add to the end of your backend function:

return SysDate;

and to your front-end code:

export function button1_click(event) {
Datestuff()
.then(r => console.log(r));
}

and check what it logs?
Are you aware that your code is supposed to provide the server time zone and not the front-end time-zone?

@jonatandor35


No change to the results but move of them :slight_smile:

Re system date is fine im using it to record a Null return on API get request so want it consistent.

@tom66859 OK, I know why it happens.
NodeJS on the backend is below version13.
and see the comment here:
“Prior to NodeJS 13, only the locale data for en-us is available by default. When other English locales are specified, the function silently falls back to en-us.”
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

@jonatandor35 :0 Thank you, that one was way beyond my understanding of Corvid code. Thank you i won’t waste my time with this piece anymore and think of a diffirent data mark.

@tom66859 You’re welcome. (And it’s not Corvid, it’s NodeJs that doesn’t fully support all JavaScript methods yet)

@jonatandor35
Depending on what Tom is using it for, it might still be possible to change it in the page code before it is displayed on the page, although probably not worth the hassle as Tom doesn’t want to use it until it is updated to 13.

Thanks for pointing this out too, as I wasn’t aware of that issue either. Having a quick look about it myself, I can see that lots of others have already been stung by it too.
https://github.com/nodejs/node/issues/8500

Funny really as I’ve looked at the page many times before and never once did I think about clicking on the dropdown arrow for Nodes to see the reason why it was starred for locales!:dizzy_face:

Now we know why and looking at it, for myself and probably Tom too, it isn’t worth the hassle of having to add additional for the ‘with full icu’ and all the extra locale data so that it is all available for people who don’t have access to 13 as of yet and want to make it work for them still.
https://nodejs.org/api/intl.html
https://techoverflow.net/2018/09/19/fixing-nodejs-intl-datetimeformat-not-formatting-properly-for-locales/

@jonatandor35 Thanks. I had exactly the same problem as Tom had and also wanted GB (which didn´t work).

@giri-zano You’re welcome, and you can always easily convert from US format to GB format:
like:

const SysDate = sd.toLocaleDateString('en-US')
let oldCom = SysDate.split("/");
let newCom = [oldCom[1].padStart(2,"0"), oldCom[0].padStart(2,"0"), oldCom[2]];
let gbFormat = newCom.join("/");

@jonatandor35 Yup, did that, although your code is more elegant.

@jonatandor35 Hey, Think i might have another one that needs your expertise. I have code that uses Promise.all again works perfectly on frontend code in a browser but put it in backend code and its a no no it just doesnt wait got sll the promises.

export function promiseall() {
let fcaarray = ;
getarray().then((result) => {
console.log(result)
console.log(result.items.length)
for ( var i = 0; i < result.items.length; i++) {
fcaarray[i] = getfcainfo(result.items[i].frn);
}
console.log(fcaarray)
Promise.all(fcaarray).then(info => {
console.log(info);
console.log(“Here”)})
});
}