【问题标题】:Custom separator in jQuery UI datetimepicker add onjQuery UI datetimepicker 中的自定义分隔符添加
【发布时间】:2016-01-15 14:19:41
【问题描述】:

我使用the datetimepicker add on for the jQuery UI datepicker(基于jQuery UI datepicker)在我的网页上实现带有日历小部件的输入字段。根据文档(参见上面链接页面上的Options 选项卡),有一个可选参数separator 可以指定介于日期和时间值之间的字符,默认为空格。

但是,当我使用不包含空格的自定义分隔符时,例如 T(在 ISO 8601 时间字符串中,看起来像 2016-01-15T15:13:20Z),我可以在小部件中选择一个时间并显示它正确,但是当我再次单击输入字段以选择下一个时,浏览器控制台给了我:

Error parsing the date string: Extra/unparsed characters found in date: T12:00:00
date string = 2016-01-13T12:00:00
date format = yy-mm-dd

是我误解了参数还是有其他方法可以实现我的自定义日期时间格式?

作为记录,我在 jQuery 2.2.0、jQuery UI 1.11.4 和 jQuery UI datetimepicker add on 1.6.1 上进行了尝试。

请参阅下面的代码和尝试自己的 sn-p。

HTML 和 JS:

// Without custom separator
$("#datetime_input_1").datetimepicker({dateFormat: "yy-mm-dd",
                                       timeFormat: "hh:mm:ss"
                                      });

// With custom separator
$("#datetime_input_2").datetimepicker({dateFormat: "yy-mm-dd",
                                       timeFormat: "hh:mm:ss",
                                       separator: "T"
                                      });
<!-- Imports -->
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/south-street/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-ui-timepicker-addon/1.6.1/jquery-ui-timepicker-addon.css" rel="stylesheet"/>


<!-- Input forms -->
<input type="text" class="form-control" id="datetime_input_1" >
<br><br>
<input type="text" class="form-control" id="datetime_input_2" >

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-datepicker datetimepicker


    【解决方案1】:

    尝试使用

    $("#datetime_input_2").datetimepicker({dateFormat: "yy-mm-dd",
                                       timeFormat: "hh:mm:ss",
                                       separator: ' T '
                                      });
    

    $("#datetime_input_2").datetimepicker({dateFormat: "yy-mm-dd",
                                       timeFormat: "hh:mm:ss",
                                       separator: " T "
                                      });
    

    代替你的

    $("#datetime_input_2").datetimepicker({dateFormat: "yy-mm-dd",
                                       timeFormat: "hh:mm:ss",
                                       separator: "T"
                                      });
    

    它应该会给你带来等待的结果。这意味着例如2016-01-22 T 12:25:25。这应该可以正常运行。

    我阅读了该插件的页面,并在 Examples 部分找到了这个

    var ex13 = $('#utility_example_1');
    
    ex13.datetimepicker({
        timeFormat: 'hh:mm tt z',
        separator: ' @ ',
        showTimezone: true
    });
    

    但我仍然不能保证字母 T 会被接受,即使它两边各有一个空格。

    编辑

    我发现(在插件代码中)这个

    /*
        * override parseDate() because UI 1.8.14 throws an error about "Extra characters"
        * An option in datapicker to ignore extra format characters would be nicer.
        */
    

    它似乎运行不正确。

    【讨论】:

    • 就我的测试而言,使用空格是有效的。但是,2016-01-22 T 12:25:252016-01-22T12:25:25 不同,因为我将字符串传递给服务器应用程序,因此很遗憾,它们将被错误地处理。当然,用"" 替换两个" " 没什么大不了的,但是,如果可能的话,我更喜欢没有变通办法的本机解决方案。
    • @Dirk:为什么要写信T?为什么不是其他角色?例如逗号 (,) 或分号 (;)。
    • 因为这些值在 GeoServer WMS 服务上的 GetMap 请求中使用。不幸的是,this is (to my knowledge) the only accepted format in GeoServer。它基于许多应用程序使用的ISO 8601,因此用T 分隔日期和时间并不少见。
    • 最近我在那个插件的代码中发现了一些非常有趣的东西。它被添加到我的答案的文本中。问题是我不知道如何修复插件中的这个错误。但可能可以避免 - 使用其他方式在输入字段中重新设置日期和时间 - 或在保存到数据库期间进行额外处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多