1

We need to figure out current user date/time format in apex code to send it to our mobile app in order to display data in same way as it is in Salesforce itself. In other words, it's about getting template like YYYY-MM-dd or whatever based on current user's locale. And we are interested specifically in template, having just formatted date/time doesn't work - need a general template which would dictate rule how to format data for our mobile app.

Any help would be appreciated. Considering also non-apex-code solution as worst case scenario.

So far the only approach we've come up with is like this - tremendously ugly:

DateTime.newInstance(1999, 11, 10, 18, 49, 37).formatLong().replace('1999', 'YYYY').replace('11', 'MM').replace('10', 'dd').replace('18', 'HH').replace('06', 'hh').replace('6', 'hh').replace('49', 'mm').replace('37', 'ss').replaceFirst('((PM|pm|AM|am)\\\\s+)(.+)', '$1z').replaceFirst('AM|am|PM|pm', 'a');
0
2

In Apex Code, DateTime.format without parameters will return the current date and time, in the user's locale formatting, in the user's current time zone, and Date.format without parameters will return the current date, in the user's locale formatting, in the user's current time zone. You may want to read the documentation for more information. If you're using something like JSON, you may want to use your mobile apps' default locale formatting, which may differ from Salesforce's settings (e.g. JavaScript's "toLocaleString" methods). There's no way to get a template string in Apex Code for the user's locale.

1
  • That's the point to have exactly the same formatting in SF and on mobile app, and reacting dynamically when it changes on SF side, picking updated one on mobile side. Yep, that what I was affraid that there is no normal way to get date/time template. As for now we've come up with this primitive approach relying on how Date API works: Date.newInstance(1999, 11, 10).format().replace('1999', 'YYYY').replace('11', 'MM').replace('10', 'dd') – wesaw Feb 5 '18 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.