Javascript jQuery 屏蔽输入 - 将日期格式化为 m/d/yyyy 或 m/dd/yyyy 或 mm/dd/yyyy 或 mm/d/yyyy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5888730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:23:05  来源:igfitidea点击:

jQuery masked input - format date as m/d/yyyy or m/dd/yyyy or mm/dd/yyyy or mm/d/yyyy

javascriptjqueryjquery-uiformatting

提问by tim2011

I want to use the jQuery masked input plugin found here http://digitalbush.com/projects/masked-input-plugin/to format the date.

我想使用http://digitalbush.com/projects/masked-input-plugin/上的 jQuery 屏蔽输入插件来格式化日期。

I am pulling data from a database to populate the date field when the page loads. The user will then be able to change the date if it is incorrect.

我正在从数据库中提取数据以在页面加载时填充日期字段。如果日期不正确,用户将能够更改日期。

I want people to be able to enter just one number for the month and day instead of having to enter a 0 before a single-digit number. I want them to be able to type 6/13/2010 instead of 06/13/2010. The data from the database might not have 0's in front, though.

我希望人们能够为月和日只输入一个数字,而不必在一位数之前输入 0。我希望他们能够输入 6/13/2010 而不是 06/13/2010。但是,来自数据库的数据前面可能没有 0。

I know that anything listed after a ? is optional, but I want characters at the beginning or middle to be optional.

我知道在 ? 是可选的,但我希望开头或中间的字符是可选的。

Thanks!

谢谢!

回答by Kevin Lee Garner

I had a similar requirement recently, and the only thing I could find that didn't require me to do a lot of work on tweaking the plugin was to use this modified date masking plugin that is based on the one you mentioned. It seems to be doing the job so far.

我最近有一个类似的要求,我能找到的唯一不需要我在调整插件上做很多工作的事情就是使用这个基于你提到的修改过的日期屏蔽插件。到目前为止,它似乎正在完成这项工作。

If you want to test it out, head to this page.

如果您想对其进行测试,请前往此页面

The basic implementation we've used with this modified date masker is as follows:

我们与这个修改后的日期掩码器一起使用的基本实现如下:

$('#yourInputBoxHere').mask("99/99/9999", {placeholder: 'MM/DD/YYYY' });

I haven't tried it with single digits for dates, but if I were to do it, I'd just change the above expression to this:

我还没有尝试过用个位数表示日期,但是如果我要这样做,我只需将上面的表达式更改为:

$('#yourInputBoxHere').mask("9/9/9999", {placeholder: 'M/D/YYYY' });

I hope this helps.

我希望这有帮助。

回答by kodare

I am not sure if this addresses your problem but I found this on https://github.com/RobinHerbots/jquery.inputmask:

我不确定这是否解决了您的问题,但我在https://github.com/RobinHerbots/jquery.inputmask上找到了这个:

$('#yourInputBoxHere').datepicker().inputmask("date", { placeholder: "mm/dd/yyyy", yearrange: { minyear: 1700 } });

This will fill in a '0' at the month beginning if you start with a number greater than '1' and for the day if the number is greater than 3, '0' is automatically placed in front.

如果您以大于“1”的数字开头,这将在月份开始时填充“0”;如果数字大于 3,则当天会自动将“0”放在前面。

The yearrangecan also specify maxyear, if you need that criteria.

yearrange还可以指定maxyear,如果你需要的标准。

You can leave out the .datepicker()if you don't want the jquery calendar.

.datepicker()如果您不想要 jquery 日历,则可以省略。

Hope this helps!

希望这可以帮助!

回答by Edgar Villegas Alvarado

The short answer is: It's not possiblewithout tweaking the plugin.

简短的回答是:不调整插件是不可能的。

But, your users will thank you a lot if you use the jquery ui datepicker.

但是,如果您使用jquery ui datepicker,您的用户会非常感谢您。

It's usage is as simple as:

它的用法很简单:

$("#texbox1").datepicker();

It will show a nice calendar when the inputbox recieves focus.

当输入框收到焦点时,它将显示一个不错的日历。

Hope this helps. Cheers

希望这可以帮助。干杯