错误的DateFormat与Jquery Datepicker [英] Wrong DateFormat with Jquery Datepicker

查看:170
本文介绍了错误的DateFormat与Jquery Datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在DateTime文本框中排除 time 部分,但是我无法按照我想要的方式工作。



这是我如何设置字段:

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

Jquery脚本

  $(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,m axDate,selectedDate);
}
});
});

现在我有这些问题:




  • 时间始终显示在字段中:






  • 日期格式是错误的:我的格式是dd / MM / yyyy,日期是 2014年11月8日但现在datepicker认为是 2014年8月10日。所以当我通过格式 dd / MM / yyyy 从datepicker中选择任意日期时,asp.net将以格式 MM / dd / yyyy



我尝试过:




  • 我试图使用 DataAnnotations [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString ={0: dd / MM / yyyy})] :不工作


  • 我尝试删除Jquery脚本, >




P / s:虽然datetime文本框包含 time 首先加载后,从datepicker选择日期后,时间部分已经消失,但日期格式错误,如上所述:



解决方案

你的日期没有正确绑定的原因是你的datepicker使用 dd / MM / yyy 格式,b你的服务器正在使用 MM / dd / yyyy 格式。要使此工作成功,您可以创建一个自定义模型binder来处理 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);
返回日期;
}
}

并在全局注册.asax (这意味着它将适用于所有 DateTime

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


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

This is how I set up 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 these problems :

  • the time always appear in the field :

  • 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

What I have tried :

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

  • I tried to remove the Jquery script, not worked

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 :

解决方案

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

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

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

这篇关于错误的DateFormat与Jquery Datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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