1

We have a Custom label that stores the value of a particular date like 11/19/2022 (mm/dd/yyyy) This label is being used in several places in the code. And we are comparing another date with this date after parsing it. if(SignedDate >= Date.parse(Label.ExpDate))

It works fine if the logged in user's locale is English(US) but is failing for other locales because of invalid date format. As different locales will have different date format. Since, there are users from different locales using the application, how can we make this generic? custom label translations won't work because that only checks the user's language and not locale.

3 Answers 3

3

You really shouldn't use labels for this sort of configuration. Salesforce provides things like Custom Metadata Types and Custom Settings for this purpose, where the field can be of Date type so no parsing would be necessary.

However, if you won't refactor to use a Custom Metadata Type or Custom Setting then you have the option to include the same date value in different locales, in the locale's format, by translating that label. E.g. for British English you would set the label translation to "19/11/2022" (dd/mm/yyyy) since this would be parsed in the user's locale format as mentioned by @kurunve.

2

In a definition of Parse method in documentation following is directly mentioned:

Constructs a Date from a String. The format of the String depends on the local date format.

Therefore it would make sense to have a custom parser developed in order ensure that it always behave in consistent way (probably using split method) and update code base to rely on it.

1

Use Date.valueOf and DateTime.valueOf instead of Date.parse and DateTime.parse. These methods always expect an ISO8601 formatted date (e.g. 2022-11-19 00:00:00), and will not change depending on locale. Note that these methods are time-zone sensitive; they will return a date and time relative to the user's time zone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .