Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!
SOLVED

Date formatting not working as expected in Campaign V8

Avatar

Level 1

Greetings experts,

I'm trying to use the out-the-box JS function .toLocaleDateString("en-GB") to format a date used in an email campaign we're building, but the resultant date is pulling through in the US format of mm/dd/yyyy instead of the British format, dd/mm/yyyy, despite having passed in the "en-GB" BCP 47 code. Has anyone encountered this in the past and found a fix?

Thanks in advance!

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi @Paulo_dev1 -

The problem you're encountering is that the .toLocaleDateString method formats the date based on the local settings of the machine running the code, rather than the locale specified in the argument. To ensure that you get the desired format, you may need to pass in additional options to the method.

I have made a couple of tests to have the date formatted as "dd/mm/yyyy".

Here's an example of how you can achieve the same:

var options = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false,
timeZone: 'Europe/London',
};

var date = new Date();
var formattedDate = date.toLocaleDateString('en-GB', options);

The options object in the above example sets the format for the date string, including the length of the day and month, the use of a 12-hour or 24-hour clock, and the time zone.

Note that the support for time zones in JavaScript is limited, and may not provide full coverage for all regions. You can also refer to the MDN web docs for more information and alternative methods.

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

Hi @Paulo_dev1 -

The problem you're encountering is that the .toLocaleDateString method formats the date based on the local settings of the machine running the code, rather than the locale specified in the argument. To ensure that you get the desired format, you may need to pass in additional options to the method.

I have made a couple of tests to have the date formatted as "dd/mm/yyyy".

Here's an example of how you can achieve the same:

var options = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false,
timeZone: 'Europe/London',
};

var date = new Date();
var formattedDate = date.toLocaleDateString('en-GB', options);

The options object in the above example sets the format for the date string, including the length of the day and month, the use of a 12-hour or 24-hour clock, and the time zone.

Note that the support for time zones in JavaScript is limited, and may not provide full coverage for all regions. You can also refer to the MDN web docs for more information and alternative methods.