简体   繁体   English

如何使用引导日期选择器自动格式化日期

[英]how to auto format date using bootstrap datepicker

how to make the datepicker auto format date after typing. 键入后如何使datepicker自动格式化日期。 eg. 例如。 type 02122018, date input will show 02/Dec/2018 upon enter or tab. 输入02122018,输入的日期将在输入或选项卡上显示02 / Dec / 2018。

The following bootstrap datepicker sandbox will default to today's date with the format i wanted. 以下引导日期选择器沙箱将默认为今天的日期,并具有我想要的格式。 But not the date i keyed. 但不是我输入日期。 https://uxsolutions.github.io/bootstrap-datepicker/?#sandbox https://uxsolutions.github.io/bootstrap-datepicker/?#sandbox

You don't really need Bootstrap datepicker for this. 您实际上并不需要Bootstrap datepicker。 You could have your input allow a delay on keyup, then look at the input and assess whether or not it is ready to be converted (aka, enough digits) and then use a certain means to interpret the format and display the converted result. 您可以让输入延迟键盘输入,然后查看输入并评估是否准备好转换(也就是足够的数字),然后使用某种方式解释格式并显示转换后的结果。

Look into momentjs for details. 有关详细信息,请查看一下momentjs

Your input 您的输入

<input id="dateInput" type="text">

Delay function 延迟功能

var delay = (function(){
    var timer = 0;
    return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    };
})();

Usage thereof 用法

jQuery(document).ready(function($) {
    $('#dateInput').keyup(function() {
        const dateString = $(this).val();
        delay(function(){
            // Use momentjs to format, for example:
            moment(dateString, "DDMMYYYY").format("MMM Do YY");
        }, 1000 );
    });
});

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM