Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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);
console.log(date);
console.log(date.toDateString());
console.log(date.toLocaleDateString());

Will result the following output:

Fri Jan 25 2013 12:59:58 GMT-0800 (PST)
Fri Jan 25 2013
1/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?

Thanks.

share|improve this question
2  
glad to see @TehNrd has made it over to the stack exchange –  Ralph 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
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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
    
Wow, this is great. It provides all sorts of formatting masks for dates and times. How did you know about this? It seems totally undocumented. –  TehNrd Feb 22 '13 at 18: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
3  
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
show 4 more comments

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

UserInfo.getLocale()

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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