输入类型日期最小值和最大值对yyyy-mm-dd而不是dd-mm-yyyy进行验证

时间:2022-09-27 09:00:52

I am using the date input type in HTML5. It is displaying the correctly in the format dd-MM-yyyy. However the min and max values does not validate correctly, because it validates against the format yyyy-MM-dd. I have not been able to change the format the min and max values validates, any ideas?

我在HTML5中使用日期输入类型。它以dd-MM-yyyy格式正确显示。但是,最小值和最大值未正确验证,因为它根据格式yyyy-MM-dd进行验证。我无法改变最小值和最大值验证的格式,任何想法?

<input type="date" name="DateName" id="DateID" min="2000-01-01" max="2100-12-31"/>

1 个解决方案

#1


16  

The attributes value, min and max have the same format for dates. They follow the RFC 3339 where the full data syntax is as:

属性值,min和max具有相同的日期格式。它们遵循RFC 3339,其中完整数据语法如下:

full-date       = date-fullyear "-" date-month "-" date-mday
date-fullyear   = 4DIGIT
date-month      = 2DIGIT  (01-12)
date-mday       = 2DIGIT  (01-28, 01-29, 01-30, 01-31)

So even if the browser displays you the date in format dd-mm-yyyy internally it's using this syntax.

因此,即使浏览器在内部以dd-mm-yyyy格式显示日期,也会使用此语法。

Anyway, this HTML5 input type is only supported in Safari, Chrome and Opera.

无论如何,这种HTML5输入类型仅在Safari,Chrome和Opera中受支持。

You can try this Fiddle with invalid dates as 12/12/2014, when you submit the form an error message is displayed.

您可以尝试使用此无效日期为12/12/2014,当您提交表单时,将显示错误消息。

#1


16  

The attributes value, min and max have the same format for dates. They follow the RFC 3339 where the full data syntax is as:

属性值,min和max具有相同的日期格式。它们遵循RFC 3339,其中完整数据语法如下:

full-date       = date-fullyear "-" date-month "-" date-mday
date-fullyear   = 4DIGIT
date-month      = 2DIGIT  (01-12)
date-mday       = 2DIGIT  (01-28, 01-29, 01-30, 01-31)

So even if the browser displays you the date in format dd-mm-yyyy internally it's using this syntax.

因此,即使浏览器在内部以dd-mm-yyyy格式显示日期,也会使用此语法。

Anyway, this HTML5 input type is only supported in Safari, Chrome and Opera.

无论如何,这种HTML5输入类型仅在Safari,Chrome和Opera中受支持。

You can try this Fiddle with invalid dates as 12/12/2014, when you submit the form an error message is displayed.

您可以尝试使用此无效日期为12/12/2014,当您提交表单时,将显示错误消息。