JDPicker正则表达式为mm / dd / YYYY格式

时间:2012-11-19 16:22:21

标签: jquery regex datepicker

我正在使用此脚本:http://jdpicker.paulds.fr/?p=doc 这是一个伟大的日期选择器,但当然,这家伙没有添加“mm / dd / YYYY”作为其中一种格式。

他声称“您可以通过编辑位于第71行和第109行之间的开关来修改或添加一些新选项。您只需要使用一些正则表达式和一些脑细胞!” - 好吧,我不是最好的正则表达式,我不能让这个工作正常。

我的代码:

case "mm/dd/YYYY": 
this.reg = new RegExp(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/); 
this.date_decode = "new Date(parseInt(matches[2]-1), matches[3], matches[1]);"; 
this.date_encode = 'this.strpad(date.getMonth()+1) + "/" + this.strpad(date.getDate()) + "/" + date.getFullYear();';
this.date_encode_s = 'this.strpad(date.getMonth()+1) + "/" + this.strpad(date.getDate())';
break;

这是一个有效的YYYY / mm / dd格式的例子:

case "YYYY/mm/dd": 
default: 
this.reg = new RegExp(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/); 
this.date_decode = "new Date(matches[1], parseInt(matches[2]-1), matches[3]);"; 
this.date_encode = 'date.getFullYear() + "/" + this.strpad(date.getMonth()+1) + "/" + this.strpad(date.getDate());'; 
this.date_encode_s = 'this.strpad(date.getMonth()+1) + "/" + this.strpad(date.getDate());'; 
break;

即使经过此后的建模,我也无法让它发挥作用。它正确地格式化日期,但最终由于某种原因将日历更改为2038年。任何帮助都会很棒!

3 个答案:

答案 0 :(得分:2)

我认为您需要更改此行:

this.date_decode = "new Date(parseInt(matches[2]-1), matches[3], matches[1]);";

到此:

this.date_decode = "new Date(matches[3], parseInt(matches[1]-1), matches[2]);";

答案 1 :(得分:0)

除了02/29之外,年份2000中的所有日期都可以使用正则表达式进行验证

(?:02\/(?:(?!00)[01]\d|2[0-8])|(?:0[469]|11)\/(?:(?!00)[0-2]\d|30)|(?:0[13578]|10|12)\/(?:(?!00)[0-2]\d|3[01]))\/2\d{3}

答案 2 :(得分:0)

看起来像一个真正的程序员改变你的正则表达式

this.reg = new RegExp(@"^\d{4}/(\d|1[0-2])/(\d|(1[\d])|(2[\d])|3[0-1])$");