简体   繁体   English

在JavaScript中使用正则表达式检查日期并根据给定类型进行转换

[英]Check The Date Using Regular Expressions In JavaScript And Converting According To The Given Type

I have requirement that a text box is filled with a date using a JQuery Date-picker and the date will be entered in any format but when it passes to a JSON webservice, 我要求文本框使用JQuery Date-picker填充一个日期,并且该日期将以任何格式输入,但是当它传递给JSON网络服务时,

  • Date must be converted to the yy-mm-dd (ex. 2014-04-15) format. 日期必须转换为yy-mm-dd(例如2014-04-15)格式。
  • This must be done using Regular expressions in JavaScript. 这必须使用JavaScript中的正则表达式来完成。 The RegEx must check the format and if it is in the right or wrong format the inserted date must be converted to the above mentioned format. RegEx必须检查格式,如果格式正确或错误,则插入日期必须转换为上述格式。

So how can I do that? 那我该怎么办呢?

yyyy-mm-dd yyyy-mm-dd

var reg=/^(?:(?!0000)[0-9]{4}[-](?:(?:0[1-9]|1[0-2])[-](?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])[-](?:29|30)|(?:0[13578]|1[02])[-]31)|(?:[0-9]{2}))?$/;

yy-mm-dd yy-mm-dd

var reg=/^(?:(?!00)[0-9]{2}[-](?:(?:0[1-9]|1[0-2])[-](?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])[-](?:29|30)|(?:0[13578]|1[02])[-]31)|(?:[0-9]{2}))?$/;

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

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