日期格式错误使用jQuery日期选择器 [英] Wrong DateFormat with Jquery Datepicker

查看:226
本文介绍了日期格式错误使用jQuery日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要排除的时间日期时间文本框的一部分,但我不能让它工作,我想要的方式。

I want to exclude the time part in the DateTime Textbox, but I can't make it work the way I want.

这是我如何设置字段:

@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脚本:

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);
            }
        });
    });

现在我有这些问题:


  • 时间总是出现在现场:

  • the time always appear in the field :


  • 日期格式是错误的:我的格式为DD / MM / YYYY,日期是 2014年11月8日但现在的日期选择器认为这是 2014年8月10日。所以,当我由格式然后从日期选择任意日期DD / MM / YYYY ,asp.net将与格式汤治疗它MM / DD / YYYY

  • the date format is wrong : my format is dd/MM/yyyy, the date is November 8th 2014 but now the datepicker "thinks" it is August 10th 2014. So when I choose an arbitrary date from the datepicker by the format dd/MM/yyyy , asp.net will treate it with the format MM/dd/yyyy

我曾尝试:


  • 我试图用 DataAnnotations [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:DD / MM / YYYY} )] :没有制定

我试图删除的jQuery脚本,没有制定

I tried to remove the Jquery script, not worked

P / S:althought日期时间文本中包含时间在第一次加载部分,从日期选择器选择一个日期后,在时间部分走了,但日期格式错误,因为我上面提到的:

P/s : althought the datetime textbox contains time part at the first load, after choosing a date from the datepicker, the time part is gone, but the date is in the wrong format as I mentioned above :

推荐答案

你为什么日期不被正确绑定的原因是你的日期选择器使用 DD / MM / YYY 格式,但您的服务器使用 MM / DD / YYYY 格式。为了使这项工作,你可以创建自定义模型绑定来处理的DateTime 值,并将其解析到你想要的格式。

The reason why you date is not being correctly bound is that your datepicker is using dd/MM/yyy format, but you server is using MM/dd/yyyy format. To make this work, you can create a custom model binder to handle DateTime values and parse them to the format you want.

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

and register it in Global.asax (this means it will apply to all DateTime values

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

这篇关于日期格式错误使用jQuery日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆