码迷,mamicode.com
首页 > Web开发 > 详细

js判断时间格式是否正确代码

时间:2015-08-13 00:51:50      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

js判断时间格式是否正确代码:

如果要求比较严格的话,时间格式也是需要进行判断的,下面就是一段这样的代码实例,希望能够给需要的朋友带来一定的帮助。

代码实例如下:

function isDateString(strDate)
{ 
  var strSeparator = "-"; 
  var strDateArray; 
  var intYear; 
  var intMonth; 
  var intDay; 
  var boolLeapYear; 
  var ErrorMsg = "";
  strDateArray = strDate.split(strSeparator); 
  if(strDateArray.length != 3) 
  { 
    ErrorMsg += "日期格式必须为: yyyy-MM-dd"; 
    return ErrorMsg; 
  } 
  intYear = parseInt(strDateArray[0],10); 
  intMonth = parseInt(strDateArray[1],10); 
  intDay = parseInt(strDateArray[2],10); 
  if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) 
  { 
    ErrorMsg += "日期格式错误: 年月日必须为纯数字"; 
    return ErrorMsg; 
  } 
  if(intMonth>12 || intMonth<1) 
  { 
    ErrorMsg += "日期格式错误: 月份必须介于1和12之间"; 
    return ErrorMsg; 
  } 
  if((intMonth==1||intMonth==3||intMonth==5||intMonth==7 ||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1)) 
  { 
    ErrorMsg += "日期格式错误: 大月的天数必须介于1到31之间"; 
    return ErrorMsg; 
  } 
  if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1)) 
  { 
    ErrorMsg += "日期格式错误: 小月的天数必须介于1到31之间"; 
    return ErrorMsg; 
  } 
  if(intMonth==2)
  { 
    if(intDay < 1) 
    { 
      ErrorMsg += "日期格式错误: 日期必须大于或等于1"; 
      return ErrorMsg; 
    } 
    boolLeapYear = false; 
    if((intYear%100) == 0)
    { 
      if((intYear%400) == 0) 
      boolLeapYear = true; 
    } 
    else
    { 
      if((intYear % 4) == 0) 
      boolLeapYear = true; 
    } 
    if(boolLeapYear)
    { 
      if(intDay > 29) 
      { 
        ErrorMsg += "日期格式错误: 闰年的2月份天数不能超过29"; 
        return ErrorMsg; 
      } 
    } 
    else
    { 
      if(intDay > 28) 
      { 
        ErrorMsg += "日期格式错误: 非闰年的2月份天数不能超过28"; 
        return ErrorMsg; 
      } 
    } 
  } 
  return ErrorMsg; 
}

原文地址是:http://www.51texiao.cn/javascriptjiaocheng/2015/0523/2309.html

最为原始的地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=9318

js判断时间格式是否正确代码

标签:

原文地址:http://www.cnblogs.com/nulifendou/p/4725910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!