Salesforce Date/Time formats in Javascript

We are making some javascript heavy pages in visualforce, which uses Remoting and Date-time fields. We need to get Date/time format salesforce is using internally in native Datepickers on object detail/edit/etc pages. Seems these date/time formats are not available to query from anywhere.

Based on some findings, we are left with one way to create a map of local > format, as described in this post below:

http://www.interactiveties.com/b_locale_datetime.php#

Or, we could hack into salesforce and use private Javascript API i.e.

var sfdcDateFormat = UserContext.dateFormat;
var sfdcDateTimeFormat =  UserContext.dateTimeFormat;

// use above variables in various Javascript datepickers and framework code as needed.

Latest “UserContext” based approach is not safe as its private API, and defining a locale map requires us to know and match all locale <> date formats with salesforce.

Anyone having a better/clean solution for this ?

Answer

Javascript provides two function which provide date user local format which are:

If these methods used perfectly date according to user local format and timezone can be shown.
But it has an issue with it. Using these methods cross-browser it show different results so these methods won’t work.

The only solution to show correct date is used moment.js. It is best library to format date and time according to language, timezone and country. It completely support automatic and manual formatting of date and time from any valid date string/instance according to local settings. Its results are consistent in all browsers. Docs: http://momentjs.com/docs/

Attribution
Source : Link , Question Author : Abhinav Gupta , Answer Author : Ashwani

Leave a Comment