错误的日期格式与 Jquery 日期选择器 [英] Wrong DateFormat with Jquery Datepicker

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

问题描述

我想排除 DateTime 文本框中的 time 部分,但我无法让它按我想要的方式工作.

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

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

现在我有这些问题:

  • time 总是出现在字段中:
  • the time always appear in the field :

  • 日期格式是错误的:我的格式是dd/MM/yyyy,日期是November 8th 2014 但现在日期选择器认为"它是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 : 虽然第一次加载时日期时间文本框包含 time 部分,但从日期选择器中选择日期后,time 部分消失了,但日期正如我上面提到的,格式错误:

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天全站免登陆