views:

7899

answers:

4

I'm using a ExtJS DateField control in an ASP.NET MVC web application.

I've set format property to "Y-m-d", so that it'll correctly parse the "2009-08-11" format from the server, and will also send it back in that format.

However, I'd like to display the data in a different, more user-friendly, format, particularly "d mmm yyyy" in Spanish.

I can't seem figure out how to do this. I don't think the altFormats property is of help, since that just adds more parse formats.

Is it possible to use a different parse format from display format? Or am I going about this the wrong way?

+2  A: 

yes. It is possible.

See http://extjs.com/deploy/dev/docs/?class=Date for date formats.

For what can be customised out of the box, see the ext-3.0.0\src\locale\ folder. Include the appropriate file or just customize your own, using on of the files as a template.

For example :

Date.monthNames = [
   "Januar",
   "Februar",
   "Marec",
   "April",
   "Maj",
   "Junij",
   "Julij",
   "Avgust",
   "September",
   "Oktober",
   "November",
   "December"
];

Date.dayNames = [
   "Nedelja",
   "Ponedeljek",
   "Torek",
   "Sreda",
   "Četrtek",
   "Petek",
   "Sobota"
];

console.log((new Date()).format('Y-M-D'))

Regards, Joshua

Joshua
Hi Joshua, I can get the date to format fine, using the ExtJs localization features. It's just that I want to use a different display format from the parsing format.
Tom Lokhorst
When we send back dates from the server, we always parse to a full javascript date. This bypasses any ambiguity possibly incurred while passing the date from SQL > C# > javascript. (C#) jsonResponse = "{mydate:new Date('" + d.ToString("MM/dd/yyyy HH:mm:ss") + "')}" ; where d is of type DateTime.Do you mean inside an EditorGridPanel or inside a FormPanel?
Joshua
The DateField is inside a Form. If it were inside a Grid, I think I could override the ColumnModel renderer to do custom rendering of the Date object.
Tom Lokhorst
You're correct about the column renderer. When setting values on your form or accepting input from your user in a different format to the display format you've set on your field, the trick is getting back to a javascript date for the parser to work correctly. If it's the latter, consider overriding the "formatDate" and "parseDate" members of the "Ext.form.DateField" with some extra processing to convert from the typed text to a javascript date. Alternatively you could consider using the datejs library in conjunction with your ExtJs implementation. See http://bit.ly/Datejs_ExtJS_Compatibility
Joshua
by the way. In response to your last comment to Lewis. If javascript is disabled, ExtJs isn't going to be available either.
Joshua
Those `formatDate` and `parseDate` on the control look very promising! I didn't know those existed, they're not in the documentation (at least not on the page I'm looking at). I'll see if I can get my desired behavior by overriding those two functions.
Tom Lokhorst
It looks like `formatDate` isn't as useful as I initially thought, this function is used for both the string that shown to the user, and the string that's send back to the server. I really want two different strings...
Tom Lokhorst
If you want to submit a different format to the server, I'd format to the desired "yyyy-mm-dd" in your json deserializer function. If you can't do that, consider overwriting the Ext.lib.Ajax.serializeForm method to handle dates in the special way that you want to do things.
Joshua
Explore the source code to see exactly what's happening when you submit your form. The Ext.lib.Ajax.serializeForm method is called from the BasicForm.js class in the "getValues" method.
Joshua
+1  A: 

Hi Tom,

So to clarify my understanding you have a date field that:

  1. Needs parsing on a server, in the format Y-m-d
  2. You want to display the date in the format d mmm yyyy using Spaninsh month names

My initial reaction, based on my understanding, would be that you could setup your ext date fields to format and validate the second format ('d mmm yyyy') using the format config option (format:'d mmm yyyy'). Then you would send that back to the server, to your controller action (I guess), as a string parameter not a DateTime. Then in your controller action parse the date string and convert it using:

 DateTime.ParseExact(myDate, 'd mmm yyyy',  CultureInfo.CurrentCulture)

or similar and then you can convert it to whatever format you like out of that date object.

This only leaves the abbreviated names in the Date fields in the browser. I'm not entirely sure how Ext handles this but I'm not aware of being able to override the date format and force it to use a specific culture, from Ext / Javascript, so I'd expect that you'd be at the mercy of the browser / OS without writing something yourself.

UPDATED:

Combined with the example given by Joshua for month / day names should give you the solution you are looking for.

I hope that helps.

Lewis
Sure, I guess that will work. I'm just hoping there's a solution that allows me to do all the culture specific formatting on the client side and send simple, non-culture specific, formats to and from the server.
Tom Lokhorst
What you need is something that intercepts the from submission and transforms the validated dates. This is fine for Javascript enabled browsers, but would you still not need to worry about JS being disabled. Maybe it's not a concern in your app? I hate dates.
Lewis
Well, if JavaScript is disabled, the user will just have to enter a date in yyyy-mm-dd format, that's fine with me (if the date is in the wrong format, the server will simply return to the page with an error message). If the user has JavaScript enabled, they get a nicer experience with a datepicker control localized to their own language. Now if there was only a way to also show the date itself in the control, in their language... :-)
Tom Lokhorst
Oh, I wasn't paying much attention to the generated HTML, apparently the date is in a CSS-hidden `<div>`, not a hidden `<input>` as I thought. In that case the whole form won't work with JavaScript disabled, but I don't really care about that, this is a commercial application, not a public facing website.
Tom Lokhorst
+3  A: 

Actually, the format config is used both for parsing AND to set the display format. As long as altFormats contains all possible data formats that you'd like to support for parsing (the default value includes Y-m-d), you should be able to do something like this:

Ext.onReady(function(){
    new Ext.form.DateField({
     format: 'd F Y', // 'd mmm yyyy' is not valid, but I assume this is close?
     width: 200,
     renderTo: Ext.getBody(),
     value: '2009-08-11'
    });
});
bmoeskau
I assume that'll display just fine. The thing is, I'd like to have `yyyy-mm-dd` as the format of the string that is returned to the server. So, I can get the parsing to work with `altFormats`, but the visual format and "send-to-server" format seem to have been merged into the single `format` property.
Tom Lokhorst
A: 

Use custom renderer function to display Spanish format properly.

Thevs