【问题标题】:Datepicker giving different result in IE and ChromeDatepicker 在 IE 和 Chrome 中给出不同的结果
【发布时间】:2019-03-20 14:02:39
【问题描述】:

我有两个 Datepicker,根据第一个 datepicker 的日期选择,第二个 datepicker 应该显示允许用户在第一个 datepicker 中选择的日期的 1 年内选择一个日期。我正在使用的代码非常适合 Chrome,但它在 IE11 中给出了不同的结果。当我使用警报方法查看日期时,在 Chrome 中我得到结果“Wed Oct 10 2018 00:00:00 GMT+0530 (India Standard Time)”而在 IE 中,结果是“Wed Oct 10 1918 00:00:00 GMT+0530 (India Standard Time)

$('.datepicker1').datepicker({
    dateFormat: "mm/dd/y",
    changeMonth: true,
    changeYear: true,
    yearRange: "-10:+10",
    showOn: "both",
    buttonText: "<i class='fa fa-calendar'></i>",
    onSelect: function (date) {


        var selectedDate = new Date(date);
        alert(selectedDate)
        var date = new Date(Date.parse(selectedDate));

        date.setFullYear(date.getFullYear() + 1);

        var newDate = date.toDateString();

        newDate = new Date(Date.parse(newDate));

        $(".datepicker2").datepicker("option", "maxDate", newDate);



    }
});

$('.datepicker2').datepicker({
    dateFormat: "mm/dd/y",
    changeMonth: true,
    changeYear: true,
    showOn: "both",
    buttonText: "<i class='fa fa-calendar'></i>"
});

下面是使用的 Razor 语法

@Html.EditorFor(m => m.date1, new { htmlAttributes = new { @class = "datepicker1 form-control", placeholder = "00/00/00", @readonly = "readonly" } })

@Html.EditorFor(m => m.date2, new { htmlAttributes = new { @class = "datepicker2 form-control", placeholder = "00/00/00", @readonly = "readonly" } })

谁能帮我弄清楚我哪里出错了?似乎 Date.Parse 在 IE 中可能无法按预期工作,我们有解决方法吗?提前致谢

【问题讨论】:

  • new Date(date)date 的值是多少?您的问题很可能是年份:dateFormat: "mm/dd/y" 应该是 dateFormat: "mm/dd/yyyy"。两位数年份被视为 20 世纪(即“18”是“1918”而不是“2018”)。
  • 附言。强烈建议不要对非标准字符串使用内置解析器。用小函数手动解析字符串或使用库,见Why does Date.parse give incorrect results?
  • @RobG 我的要求是以 mm/dd/y 格式显示日期,即 10/16/18 根据当前日期。如果我使用 mm/dd/yy,我将不会遇到问题中提到的问题。截至目前,我将尝试手动解析它并进行更新。
  • 酷,似乎 UI 呈现方式妨碍了解析和处理。解析器是 2 行代码:拆分然后将位提供给 Date 构造函数,例如let b = s.split(/\D/); return new Date('20' + b[2], b[0]-1, b[1]) 假设所有年份都是 21 世纪,并且 s 是一个类似于 2009 年 9 月 14 日的“09/14/09”的字符串。
  • @RobG 帮了大忙。因为我不会处理过去的日期,所以这个解决方案适合我。

标签: javascript jquery asp.net-mvc date datepicker


【解决方案1】:

2 位日期转换导致 Date.parse()(和 Date 构造函数)出现 20 世纪问题是 IE(和 Edge,请参阅 this issue)中的已知行为,如下所示:

Firefox 49 更改了 2 位数年份的解析以与 谷歌 Chrome 浏览器而不是 Internet Explorer。现在,两位数的年份 小于 50 的将被解析为 21 世纪。

至于 IE 11 和 Edge 中的解决方法,您可以通过使用 momentjs 将 2 位数年份格式转换为 4 位数年份来避免 Date 构造函数解析日期字符串,如下所示:

var date = new Date(moment(selectedDate, "M/D/YY").format("M/D/YYYY"));

newDate = new Date(moment(newDate, "M/D/YY").format("M/D/YYYY"));

请注意,您仍然可以为 2 到 4 位数字年份转换创建自己的解析器,但拆分日期组件并将它们重新加入 Date 构造函数可能需要更多时间。

相关问题:

Converting (formatting) 2-digit date to a 4-digit date

【讨论】:

    【解决方案2】:

    Rob 实际上帮助我找到了解决方案,因为他在 cmets 中发布了答案,我将其作为单独的答案提及。我的要求是将日期显示为 mm/dd/yy 格式,但 2 位数年份在 IE 中被视为 20 世纪,这就是为什么我在 18 年 10 月 16 日在 IE 中得到 1918 的原因。所以我按照 Rob 的建议写了一个小的解析器方法。

    function parseDate(input) {
        var parts = input.split('/');
        // new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
        return new Date('20' + parts[2], parts[0] - 1, parts[1]);
    }
    

    在第一个日期选择器的onSelect事件中使用方法

    onSelect: function (date) {
    
            var parsedDate = parseDate(date);
    
            var date = new Date(Date.parse(parsedDate));
    
            date.setFullYear(date.getFullYear() + 1);
    
            var newDate = date.toDateString();
    
            newDate = new Date(Date.parse(newDate));
    
            $(".datepicker2").datepicker("option", "maxDate", newDate);
    
    
        }
    

    由于我只处理当前日期,我不必担心属于 1900 年代的日期。

    【讨论】:

    • 根据ECMA-262 §20.3.2.1 4.h: If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y); 将 2 位数年份视为 20c。 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多