function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BAGELBAGEL 

Is Date field format customizable or fixed?

Are all dated fields in "DD/MM/YYYY" format? Or this is customizable? Or it is based on your browser setting?
SuperfellSuperfell
Its based on your locale settings for your login in the app.
BAGELBAGEL
Ok, in this case, if I have custom object that contain a date field. After I query a record using Ajax toolkit. That date field become a Date object in the recordset. So is there a function or something to display it according to the locale setting? How do I do that? And If I need to update this field, how do I save it back to the database?
Ron HessRon Hess

at the top of the file, throw up a global

Code:
var dformat; // holds the format string that we think date should be shown with

then later, somewhere near sforceClient.init()
Code:
sforceClient.init("{!API_Session_ID}", "{!API_Partner_Server_URL_70}");
var uInfo = sforceClient.GetUserInfo(); // has the locale
dformat = Sforce.Util.ParseDateFormat("{!Today}",uInfo.userLocale);

// create a date using Locale format
var l_textField = Sforce.Util.FormatDate (so.get('CreatedDate'), dformat + " HH:mm") // use dformat


Code:
/* ParseDateFormat ( "26/02/2006" ) == "d/M/y"
* given a date string, report which format it is in,
* used to detect locale date formats
*/
Sforce.Util.ParseDateFormat = function(val,lang) {lang = lang+"";
/*Lang comes from API Name: LocaleSidKey Type: picklist */
var preferEuro = true;
if ( /US/.test(lang) ) preferEuro = false;
generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
dateFirst =new Array('d/MM/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
var checkList= new Array(generalFormats,preferEuro—dateFirst:monthFirst,preferEuro–monthFirst:dateFirst);
var d=null;
for (var i=0; i<checkList.length; i++) {
var l=checkList[i];
for (var j=0; j<l.length; j++) {
d=Sforce.Util.GetDateFromFormat(val,l[j]);
if (d!=0) { return l[j] }
}
}
return "M/d/y"; // a default

}


I could just write a function which will return a format for each and every userLocale supported, but i didn't for some shortsighted reason i can't recall. 
if anyone has / writes a UserLocale --> date format using Switch(userLocale) { case ....  rountine, please post, i think that would be the real way to go.
To store a date back into a dynabean, there is a function in the toolkit called Sforce.Util.ParseDate()  which looks a lot like the routine above but returns a date object which dynabean.set() can accept.

Message Edited by Ron Hess on 04-03-2006 09:53 PM

BAGELBAGEL
Thank you.
Carl_VCarl_V
Can this approach be used to overcome a problem I have with locales at the moment?

If my user is of US locale, then when they enter a date into s-control via calendar widget all is OK.
If however they are of locale that uses DD-MM-YYYY date format, then the date entered via the widget is parsed incorrectly, and day and month are switched.

Dont know how to handle this situation - any suggestions?

Regards,
Carl_V


Ron HessRon Hess
yes, you pass the users locale into that parse routine so it can properly guess the format that the user will enter the date in.


Carl_VCarl_V
Im having trouble understanding how or what this does Im sorry - can I use this routine to take a date string from user locale eu_AU, and convert it to eu_US?
The_FoxThe_Fox
Hi Ron,

Just a little question for my culture
 can you explain this ligne of code
if ( /US/.test(lang) )

I am using your code plus something I have crafted myself but it is to understand

Thanks for an update on this.

Regards
It is important to know that javascript is considering all date by default as mm/dd/yyyy

Just for you:smileyvery-happy:
Code: function Return_LocaleDate(p_string) {
    var date_order= dformat.split(/\W/);
    switch (date_order[0].charAt(0).toLowerCase()) {
        case 'm':
            A = p_string.split(/\D/);
            return(new Date(A[2],A[0]-1,A[1]));
            break;
        case 'd':
            A = p_string.split(/\D/);
            return(new Date(A[2],A[1]-1,A[0]));
            break;
        default: return null;
    }
}
Bye
Ron HessRon Hess
No, it's not that smart yet, but it will return the format string "dd/mm/yyyy" or whatever that it believes the date passed in was.

Then you can use the DateFormat() to generate other dates (usint dates in beans) which are in the users' locale format.

so it's almost what you want, but only returns the format.  
Carl_VCarl_V
Thanks
KSFDCKSFDC

Hi All,

I am facing the same problem.We have generalised s-control to use MM/dd/yyyy format and ask our users to change regional settings date format.

Applicaton behaves smoothly for all US users.Recently one of our users faced problem of date widget converting the date into dd/mm/yyyyy.He has verified that he sticks to the date format that is generalised for our application user.

has anybody clue on this?

We are not using Ajax tool kit for S-control development.our application is purely HTML & Javascript dependent with SOAP & XML messaging.Can somebody help out!

Thanks,

K-SFDC developer.