社区
JavaScript
帖子详情
200分求助日期检测问题,高手帮忙
yacus
2003-09-19 04:31:45
求一个日期检测函数.
一个合法日期的任意左端也合法
如
2000-02-2 合法
2 合法
3 不合法
20 合法
2003 合法
2003-2 合法
函数格式如下
function checkDate (date){
合法返回 true;
}
...全文
35
11
打赏
收藏
200分求助日期检测问题,高手帮忙
求一个日期检测函数. 一个合法日期的任意左端也合法 如 2000-02-2 合法 2 合法 3 不合法 20 合法 2003 合法 2003-2 合法 函数格式如下 function checkDate (date){ 合法返回 true; }
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
piccologoo
2003-10-10
打赏
举报
回复
function chkDate(strValue)
{
strValue=new String(strValue);
var Reg;
var iDotPos,iYear,strRight,iMonth,iDay;
Reg=/^(\d{2}|\d{4})\/(\d{1}|\d{2})\/(\d{1}|\d{2})$/;
if(Reg.test(strValue))
{
iDotPos=strValue.search("\/");
iYear=strValue.substring(0,iDotPos);
strRight=strValue.substring(iDotPos+1,strValue.length);
iDotPos=strRight.search("\/");
iMonth=strRight.substring(0,iDotPos);
iDay=strRight.substring(iDotPos+1,strRight.length);
if(iMonth>12||iMonth==0)
{
return false;
}
if(iMonth==2)
{
if (iYear<100) iYear+=2000;
if ((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0)
{
if(iDay>29||iDay==0) return false;
}
else
{
if(iDay>28||iDay==0) return false;
}
return true;
}
else if(iMonth==1 || iMonth==3 || iMonth == 5 ||iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12)
{
if(iDay>31||iDay==0)
{
return false;
}
}
else
{
if(iDay>30||iDay==0)
{
return false;
}
}
return true;
}
yacus
2003-10-10
打赏
举报
回复
谢谢楼上的诸位热心帮助,但是都不能测试通过!不过我受到你们的启发了.^_^
此问题其实超简单!挺有趣的,大家开动脑筋,别按照以往固定的解题思路,可以走捷径的.^_^这个乐趣留给大家,明天告诉大家我的方法.
用户如果依次输入合法日期,函数会检测通过.
如在输入一下数值时都合法:
2
20
200
2003
2003-
2003-2
2003-02
2003-2-6
rongwenfeng
2003-10-09
打赏
举报
回复
function checkDate( date ) {
if ( date == null || date == "" ) {
return true;
}
var strDate = date;
if (NumberFormat( strDate ))
{
if (strDate.length != 10) {
alert( "错误的日期!" );
return false ;
}
if ( toDateFromString( strDate ) == null ) {
alert("错误的日期!");
return false;
}
}
return true ;
}
function NumberFormat(pstrNum)
{
var strReturn = "";
strNumber = ""+delMoji(pstrNum,"-");
for(var i=0; i<strNumber.length; i++)
{
if (strNumber.charAt(i) >= "0" && strNumber.charAt(i) <= "9")
{
}
else if (strNumber.charAt(i) == "+" || strNumber.charAt(i) == "-" || strNumber.charAt(i) == ".")
{
alert("错误的日期!");
return false;
}
}
return true;
}
function delMoji(pstrMoji, pstrChar)
{
var strReturn ="";
for(var i=0; i<pstrMoji.length; i++)
{
if (pstrMoji.charAt(i) != pstrChar)
{
strReturn = strReturn + pstrMoji.charAt(i);
}
}
return strReturn;
}
function toDateFromString( strDate )
{
var dtDate = null ;
var nYear = parseInt( strDate.substring( 0, 4 ), 10 ) ;
var nMonth = parseInt( strDate.substring( 5, 7 ), 10 ) ;
var nDay = parseInt( strDate.substring( 8, 10 ), 10 ) ;
if( isNaN( nYear ) == true || isNaN( nMonth ) == true || isNaN( nDay ) == true )
{
return null ;
}
dtDate = new Date( nYear, nMonth - 1, nDay ) ;
if( nYear != dtDate.getFullYear() || ( nMonth - 1 ) != dtDate.getMonth() || nDay != dtDate.getDate() )
{
return null ;
}
return dtDate ;
}
longshenwang
2003-10-09
打赏
举报
回复
关注。。。。
junyi2003
2003-10-09
打赏
举报
回复
关注一下。
yanfeng
2003-09-21
打赏
举报
回复
呵呵,都被别人说了。顶一下
yacus
2003-09-21
打赏
举报
回复
问题都没有解决哦
shyslysky
2003-09-21
打赏
举报
回复
function checkDate (date)
{
var strValue = new String();
strValue = date;
var year = new String();
var month = new String();
var day = new String();
switch(strValue.length)
{
case 4:
{
year = strValue;
var testDate = new Date(year,1,1);
return (year == testDate.getFullYear());
break;
}
case 7:
{
if (strValue.charAt(4)!='-')
{
return false;
}
else
{
strValue = value;
year = strValue.substr(0,4);
month = strValue.substr(5,2);
month = month-1;
var testDate = new Date(year,month,1)
return ((year == testDate.getFullYear()) && (month ==testDate.getMonth()));
}
break;
}
case 10:
{
if (strValue.charAt(4)!='-'||strValue.charAt(7)!='-')
{
return false;
}
else
{
year = strValue.substr(0,4);
month = strValue.substr(5,2);
month = month-1;
day = strValue.substr(8,2);
var testDate = new Date(year,month,day)
return ((year == testDate.getFullYear()) && (month ==testDate.getMonth())&&(day == testDate.getDate()));
}
break;
}
default:
return false;
}
}
gaozyr
2003-09-19
打赏
举报
回复
这个用正则比较好些!!
"if(event.srcElement.value!=''){" & _
"var zz=/^(\d{4})[-, ,|,\/,\\,\.,\,](\d{1,2})[-, ,|,\/,\\,\.\,](\d{1,2})$/;" & _
"if(zz.test(event.srcElement.value)){" & _
"var kk=/^(\d{2})$/;" & _
"var strYear=event.srcElement.value.substring(0,4);" & _
"var strMonth=event.srcElement.value.substring(5,7);" & _
"var strDay=event.srcElement.value.substring(event.srcElement.value.length-2,event.srcElement.value.length);" & _
"if(!kk.test(strMonth)){" & _
"strMonth=event.srcElement.value.substring(5,6);}" & _
"if(!kk.test(strDay)){" & _
"strDay=event.srcElement.value.substring(event.srcElement.value.length-1,event.srcElement.value.length);}" & _
"if(strYear<1900||strYear>2099){" & _
"alert('输入日期的年份超出范围!');" & _
"event.srcElement.focus();}" & _
"else{" & _
"if(strMonth<1||strMonth>12){" & _
"alert('输入日期的月份超出范围!');" & _
"event.srcElement.focus();}" & _
"else{" & _
"var strLastDay;" & _
"switch(strMonth){" & _
"case '2':if(strYear%4==0){strLastDay=28;break;}else{strLastDay=29;break;}" & _
"case '4':{strLastDay=30;break;}" & _
"case '6':{strLastDay=30;break;}" & _
"case '9':{strLastDay=30;break;}" & _
"case '11':{strLastDay=30;break;}" & _
"default:{strLastDay=31;break;}}" & _
"if(strDay>strLastDay||strDay==0){" & _
"alert('输入日期超出范围!');" & _
"event.srcElement.focus();}" & _
"else{" & _
"var strDate=new Date(strYear,strMonth-1,strDay);" & _
"if(strDate=='NaN'){" & _
"alert('输入日期的逻辑有误!');" & _
"event.srcElement.focus();}" & _
"else{" & _
"event.srcElement.className='textbox';}" & _
"}" & _
"}" & _
"}" & _
"}" & _
"else{" & _
"alert('请输入正确的日期类型!(YYYY-MM-DD)');" & _
"event.srcElement.focus();}" & _
"}" & _
"else{" & _
"event.srcElement.className='textbox';}"
Output.AddAttribute("onblur", strJS)
通过这个你自已想想就OK了!!
yacus
2003-09-19
打赏
举报
回复
楼上的,没符合要求哦.20是合法的.2003-0 也是合法的哦
007james
2003-09-19
打赏
举报
回复
给你个参考:
function sTrim(str){
var i,j,s;
if(str == null){
return '';
}
if(str.length == 0){
return '';
}
for(i = 0;i < str.length;i ++){
if(str.charAt(i) != ' '){
break;
}
}
if(i >= str.length){
return '';
}
for(j = str.length - 1;j >= 0;j --){
if(str.charAt(j) != ' '){
break;
}
}
s = str.substring(i,j + 1);
return s;
}
function IsDate(str1,str2,str3){
if(parseInt(sTrim(str1))){
if(sTrim(str1).length==4){
var yy=parseInt(str1);
var isY=0;
if(((yy%100!=0)&&(yy%4==0))||(yy%400==0)){
isY=1;
}else{
isY=0;
}
if(isY==1){
switch(parseInt(str2)){
case 2:
if(parseInt(str3)>29){
alert("二月没这一天。");
return false;
}
case 4:
if(parseInt(str3)>30){
alert("四月没这一天。");
return false;
}
case 6:
if(parseInt(str3)>30){
alert("六月没这一天。");
return false;
}
case 9:
if(parseInt(str3)>30){
alert("九月没这一天。");
return false;
}
case 11:
if(parseInt(str3)>30){
alert("十一月没这一天。");
return false;
}
};
}else{
switch(parseInt(str2)){
case 2:
if(parseInt(str3)>28){
alert("二月没这一天。");
return false;
}
case 4:
if(parseInt(str3)>30){
alert("四月没这一天。");
return false;
}
case 6:
if(parseInt(str3)>30){
alert("六月没这一天。");
return false;
}
case 9:
if(parseInt(str3)>30){
alert("九月没这一天。");
return false;
}
case 11:
if(parseInt(str3)>30){
alert("十一月没这一天。");
return false;
}
};
}
return true;
}else{
alert("年份为四位数(如:2001)。");
return false;
}
}else{
alert("请输入正确的年份。");
return false;
}
}
function dateKeyEvent(edit) {
var sDate = sTrim(edit.value);
var m_day = new Array();
m_day[0] = 31;
m_day[1] = 28;
m_day[2] = 31;
m_day[3] = 30;
m_day[4] = 31;
m_day[5] = 30;
m_day[6] = 31;
m_day[7] = 31;
m_day[8] = 30;
m_day[9] = 31;
m_day[10] = 30;
m_day[11] = 31;
var ch = sDate.charAt(sDate.length - 1);
if((sDate.length < 5) || ((sDate.length > 5) && (sDate.length < 8)) || ((sDate.length > 8) && (sDate.length < 11))){
if((ch < '0') || (ch > '9')){
edit.value = sDate.substring(0,sDate.length - 1);
return;
}
}
if((sDate.length == 4) || (sDate.length == 7)){
sDate = sDate+"-";
edit.value = sDate;
}
if(sDate.length == 10){
var year = parseInt(sDate.substring(0,4),10);
var month = parseInt(sDate.substring(5,7),10);
if((month > 12) || (month < 1)){
sDate = sDate.substring(0,5);
edit.value = "'"+month+"'";
return;
} else {
var day = parseInt(sDate.substring(8),10);
if(month == 2){
if((year % 400) == 0){
if((day < 1) || (day > (m_day[month - 1] + 1))){
sDate = sDate.substring(0,8);
edit.value = sDate;
return;
}
} else {
if(((year % 4) == 0) && ((year % 100) != 0)){
if((day < 1) || (day > (m_day[month - 1] + 1))){
sDate = sDate.substring(0,8);
edit.value = sDate;
return;
}
} else {
if((day < 1) || (day > (m_day[month - 1]))){
sDate = sDate.substring(0,8);
edit.value = sDate;
return;
}
}
}
} else {
if((day < 1) || (day > m_day[month - 1])){
sDate = sDate.substring(0,8);
edit.value = sDate;
return;
}
}
}
} else {
if(sDate.length > 10){
sDate = sDate.substring(0,10);
edit.value = sDate;
}
}
}
function dateBlurEvent(edit) {
var sDate = sTrim(edit.value);
var m_day = new Array();
m_day[0] = 31;
m_day[1] = 28;
m_day[2] = 31;
m_day[3] = 30;
m_day[4] = 31;
m_day[5] = 30;
m_day[6] = 31;
m_day[7] = 31;
m_day[8] = 30;
m_day[9] = 31;
m_day[10] = 30;
m_day[11] = 31;
var ch;
if(sDate.length != 10){
edit.value = "";
return;
}
for(i = 0;i < 10;i ++){
ch = sDate.charAt(i);
if(((i >= 0) && (i <= 3)) || ((i >= 5) && (i <= 6)) || ((i >= 8) && (i <= 9))){
if((ch < '0') || (ch > '9')){
edit.value = "";
return;
}
} else {
if((i == 4) || (i == 7)){
if(ch != '-'){
edit.value = "";
return;
}
}
}
}
var year = parseInt(sDate.substring(0,4),10);
var month = parseInt(sDate.substring(5,7),10);
var day = parseInt(sDate.substring(8),10);
if((month > 12) || (month < 1)){
edit.value = "";
return;
}
if(month == 2){
if((year % 400) == 0){
if((day < 1) || (day > (m_day[month - 1] + 1))){
edit.value = "";
return;
}
} else {
if(((year % 4) == 0) && ((year % 100) != 0)){
if((day < 1) || (day > (m_day[month - 1] + 1))){
edit.value = "";
return;
}
} else {
if((day < 1) || (day > (m_day[month - 1]))){
edit.value = "";
return;
}
}
}
} else {
if((day < 1) || (day > m_day[month - 1])){
edit.value = "";
return;
}
}
}
dateparser终极指南:如何轻松解析
200
+语言的人类可读
日期
本文全面介绍Python库dateparser的核心能力:支持
200
+语言的人类可读
日期
智能解析,涵盖多语言识别、相对/绝对
日期
处理、时区感知、文本内
日期
搜索及高级配置。重点说明安装方法、Settings参数调优、性能优化策略(如实例复用与语言限定)以及防误解析实践,适用于数据爬虫、NLP交互和国际化应用等场景。
【制造业&流水线】包装袋与
日期
码区域
检测
系统源码&数据集全套:改进yolo11-DWR
随着电商和物流发展,包装袋与
日期
码自动
检测
需求增大。本项目基于改进的YOLOv11模型构建
检测
系统,适配多种模型和识别模式。介绍了原始YOLOv11算法,讲解
200
+种改进创新点原理,还提供项目环境部署、训练教程及核心改进源码讲解,可从原始博客获取完整资源。
打印100到
200
之间的素数及输出素数的个数;编写判别素数的函数,主函数输出
200
以内的素数(C)
本文介绍如何使用C语言判断素数,并提供两个示例程序:一是打印100到
200
之间的所有素数及其数量;二是输出
200
以内的全部素数。通过这些示例,读者可以了解素数的基本概念以及高效的素数
检测
算法。
蓝桥杯C++——试题 历届试题
日期
问题
本文针对蓝桥杯C++编程竞赛中的
日期
解析
问题
,提供了详细的算法思路和参考代码实现。主要讨论如何处理格式混乱的历史文献
日期
,转换为标准的yyyy-MM-dd格式,并按时间顺序输出所有可能的正确
日期
。
intel ax
200
ngw刷killer 1650x教程
该博客提供intel ax
200
ngw刷killer 1650x的教程。因win10
200
4版本更改判断方式,此版本用户需直接跳到第七步。教程包含从killer官网下载控制中心、修改注册表、更换驱动、编辑代码、重启到禁用驱动签名模式等多步骤,最终实现ax
200
刷为1650x。
JavaScript
87,987
社区成员
224,683
社区内容
发帖
与我相关
我的任务
JavaScript
Web 开发 JavaScript
复制链接
扫一扫
分享
社区描述
Web 开发 JavaScript
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章