DateTimeFor format

Nov 24, 2011 at 9:54 AM


I try to format a display for but I keep on getting the default format. Probably I'm doing something wrong.

In my viewI have

var datecalendar = Html.DateTimeFor(x => x.EndDate, emptyDate: DateTime.Now, dateInCalendar: true);
@datecalendar.DateCalendar(inLine: false, calendarOptions: new CalendarOptions { ChangeYear = true, ChangeMonth = true })


In my DTO I have

[DataType(DataType.Date, ErrorMessageResourceName = "Date_is_invalid", ErrorMessageResourceType = typeof(Global))]
[Format(ExtractClientFormat = true,ClientFormat = "dd-mm-yyyy")]
public DateTime? StartDate { get; set; }


What could it be?

Nov 24, 2011 at 1:01 PM

At moment the ClientFormat attribute is not supported by the DateCalendar. If you use the tree comboboxes option (Date(....) method) you can supply a culture info containing the needed information....but not for the date calendar.


HOWEVER all controls supports globalization, so if you set up globalization as explained here the DateTimeFor will use the right format for the selected culture. You can choose the culture setting in  the <globalization/> tag of the web.config culture="it-IT"...oir whatever, if you set culture="auto" (this is the default option) the culture will be autodetected from the culture set in the browser.

Nov 24, 2011 at 4:01 PM

The thing is that the application need - instead of / as date seperator... Anything to do this?

Nov 24, 2011 at 11:02 PM

You have to put immediately adter the gloabalization script for the UI datepicker (@Html.JQueryDatePickerGlobalizationScript()) the following code

dateFormat: 'dd-mm-yyyy'
The above will change the format of all datepicker in the page
Nov 25, 2011 at 11:02 AM

Thanks, that did the trick!

Nov 25, 2011 at 10:11 PM

Anyway, in the next release I will add a DateFormat property in the CalendarOption class

Nov 27, 2011 at 12:45 PM

I implemented full support for the ClientFormat of date in both TypedTextBox withe date and DateTimeInput. It will be available in the upcoming release 1.6. One can use both an explicit format, such as "yyyy-MM-dd" or a standard format such as "d" or "D". See here: Your problem can be solved with the standard format "S".

Instead of using the ClientFormat one can choose also to pass the DateFormt as a property of the CalendarOprion object.


Pls Note ExtractClientFormat = true just makes the ClientFormat to be extracted from the server side DataFormat that is contained in the same attribute,so if one specifies explicitely ClientFormat there is no need to set it to true. The reverse is always true. That is the sever side DataFormat is alway extracted from the ClientFormat, if the first is not available and the seconde one is available.

Dec 2, 2011 at 11:24 AM

Hi, I added support for the ClientFormat attribute in the new 1.6 release of the Mvc Controls toolkit. Let me know it this is usefull to you. For the way to use it, pls read my previous post or the updated documentation on both the FormatAttribite and the DateTimeInput.

If this feature was helpfull to you and if you like the Mvc Controls Toolkit, pls take some time to give me a 5 star review both on codeplex and on nuget.