18

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 ?

3 Answers 3

1

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/

5
  • 2
    It is a bit trickier with Salesforce, because Users may have locales and timezones that are different than the JavaScript functions would return (i.e., User could have different timezone/locale set on their User record in SFDC than what the browser would detect). Commented Mar 29, 2014 at 11:53
  • Thanks @Regal, we are already using MomentJS, but this lib needs correct dose of salesforce user's timezone and date format to work correctly. Commented Mar 29, 2014 at 20:09
  • Agree with @PeterKnolle, we are trying to resolve such issues, as they go terrible in Javascript, when browser/os timezone/locale differs from Salesforce user's timezone/locale. That will be another big discussion altogether, which is out of scope for this questions, so not adding any more here. For now key for me is to get salesforce formats correctly. Commented Mar 29, 2014 at 20:12
  • @AbhinavGupta I agree that it is trickier to use. But I believe this the best solution right now. Even Salesforce uses moment.js in their Salesforce1 app.
    – Ashwani
    Commented Apr 14, 2014 at 6:54
  • Thanks @regal, good to know about salesforce1 using moment.js. Commented Apr 15, 2014 at 9:52
0

I don't get the meaning of statement ("UserContext" based approach is not safe as its private API). As UserContext variable is defined in java-script by Salesforce itself. So you can get dateTimeFormate & locale by UserContext.dateTimeFormat, UserContext.locale and pass in the method getTimeZoneValue() defined in the above blog url.

Thanks

1
  • 2
    UserContext is not officially documented and exposed by Salesforce. So its not safe to use, if any day that changes in a release, all my pages will blew up. Commented Mar 28, 2014 at 8:22
0

Why dont you just use the standard DatePicker Salesforce has ..?? We have done that and irrespective of the userlocal the user has .. The Datepicker will render Datepicker in that format.. All you need to do is

1) You need to have on the VF page 2) your input tag (To accept date from user selection)

Assuming below is the Apex page

<apex:page> 

 <input type = "text" onfocus = "showDatePicker(this)" />
<script>
//Some javascript to handle the datepicker..

function showDatePicker(thisPtr) {
DatePicker.pickDate(true, thisPtr.id, false);
}
</script>

<apex:form>
</apex:form>
</apex:page>

Thats all..

1
  • If you are using certain js framework but not use any sf page tags, then you will have to format the number and date manaully.
    – Leisure
    Commented Mar 8, 2016 at 7:42

You must log in to answer this question.

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