Invalid DateFormat with jQuery Datepicker

I want to exclude a part timein the DateTime text box, but I cannot make it work the way I want.

This is how I set the field:

@Html.TextBoxFor(m => m.DateFrom, "{0:dd/MM/yyyy}", new { @class = "datefrom" })
@Html.TextBoxFor(m => m.DateTo, "{0:dd/MM/yyyy}", new { @class = "dateto" })

JQuery script:

    $(function () {
        $(".datefrom").datepicker({
            defaultDate: "+1w",
            changeMonth: true,  
            numberOfMonths: 1,
            dateFormat: "dd/mm/yy",
            onClose: function (selectedDate) {
                $("#to").datepicker("option", "minDate", selectedDate);
            }
        });
        $(".dateto").datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            dateFormat: "dd/mm/yy",
            onClose: function (selectedDate) {
                $("#from").datepicker("option", "maxDate", selectedDate);
            }
        });
    });

Now I have the following problems:

  • time always displayed in the field:

enter image description here

  • Invalid date format: my format is dd / MM / yyyy, a date November 8th 2014, but now the date picker “thinks” that it is August 10th 2014. Therefore, when I select an arbitrary date from a datepicker in the format dd/MM/yyyy, asp.net will process it in the formatMM/dd/yyyy

What I tried:

  • I tried to use DataAnnotations: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]not working

  • I tried to remove the jQuery script, did not work

P/s: datetime time , datepicker time , , :

enter image description here

+1
1

, , , datepicker dd/MM/yyy, MM/dd/yyyy. , DateTime .

public class CustomDateTimeBinder : IModelBinder
{
  public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  {
    var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    CultureInfo culture = new CultureInfo("en-GB"); // dd/MM/yyyy
    var date = value.ConvertTo(typeof(DateTime), culture);
    return date;
  }
}

Global.asax ( , DateTime

ModelBinders.Binders.Add(typeof(DateTime), new YourAssembly.CustomDateTimeBinder());
+1

All Articles