If you query a Date or a DateTime field with JavaScript Remoting the value returned will be the number of milliseconds since 1970 such as, 1359147598000. Good for data consistency...bad for user experience.

var date = new Date(1359147598000);

Will result the following output:

Fri Jan 25 2013 12:59:58 GMT-0800 (PST)
Fri Jan 25 2013

For United States users the date should format as 1/25/2013. For the mates in UK it should format as 25/01/2013.

You would think toLocaleDateString() would be the solution but it appears very brittle and it doesn't even seem to work for when I change my computers locale.

So what is the best way to format and display Date/DateTime values in the users local format when using JavaScript Remoting?


  • 2
    glad to see @TehNrd has made it over to the stack exchange – Ralph Callaway Feb 19 '13 at 15:51
  • I asked a closely-related question and got a really nice answer. salesforce.stackexchange.com/questions/8540/… – Shane McLaughlin Feb 19 '13 at 22:40
  • In addition to window.UserContext having a JS-ready string for the user's dateFormat on it, you might also take a peek at the functions available within window.DateUtil, there may be some useful bits there too. For instance, converting and parsing dates and detecting if the user's browser timezone is the same as their SFDC user-record timezone. DateUtil.isBrowserAndSystemTimezoneSame(); – Mark Pond Feb 19 '13 at 23:17
  • Do you want the "user's local format" to be what is defined in the user's SFDC profile, or what the user's browser/OS says? Will something like this help: github.com/mde/timezone-js – jkraybill Feb 20 '13 at 4:44

I'd recommend any JavaScript date library in conjunction with UserContext.dateFormat. As you say, browser locale implementations are pretty brittle. The display on the client should be completely independent of the underlying value and whether or not it was remoted :-)

Edit: or carefully take advantage of the DateUtil object already present (courtesy @TehNrd)

From the comments: DateUtil.getDateTimeStringFromUserLocale(d); and DateUtil.getDateStringFromUserLocale(d); I'd recommend wrapping them in a try catch as they could change at anytime.

  • What do you mean but UserContext.dateFormat? I'm not seeing this in JavaScript or Apex. – TehNrd Feb 19 '13 at 16:57
  • 2
    UserContext is an SFDC populated JavaScript object available on the window object. for (var key in window.UserContext) { console.log('' + key + ': ' + window.UserContext[key]); } – Mark Pond Feb 19 '13 at 22:43
  • 1
    These two methods are actually even better: DateUtil.getDateTimeStringFromUserLocale(d); and DateUtil.getDateStringFromUserLocale(d); Id recommend wrapping them in a try catch as they could change at anytime. – TehNrd Feb 22 '13 at 19:55
  • 4
    It would be great to clarify that the reason DateUtil and UserContext could change at any time is because they are intended for internal use. See the warning, "This code is for Internal Salesforce use only, and subject to change without notice. Customers shouldn't reference this file in any web page." – Carolyn Grabill Feb 23 '13 at 1:14
  • 1
    @Carolyn, i see you work at SFDC. In the near future if not now it is going to be vital for SFDC to provide this type of supported functionality as more and more apps move back to the client. – TehNrd Feb 25 '13 at 0:59

Use DateFormat.js to convert date into any user locale format. To get the user locale in apex


  • Unfortunately getLocale() appears to return a non standard value and this makes if very difficult to work with a tool like date.js or jQuery Globalize. success.salesforce.com/ideaView?id=08730000000kehZAAQ – TehNrd Feb 19 '13 at 19:08
  • Yeah its true. But you can support most of the locales using getLocale(). We almost worked to support most locales which is there in salesforce recently it worked! – Ugesh Gali Feb 20 '13 at 3:45

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.