JavaScript 判斷日期格式是否正確的實(shí)現(xiàn)代碼
更新時(shí)間:2011年07月04日 23:42:41 作者:
沒(méi)有多大變動(dòng),主要是返回錯(cuò)誤信息,以便調(diào)用函數(shù)部分可以alert出來(lái)。據(jù)說(shuō)可以用正則表達(dá)式校驗(yàn),下次再研究下。
轉(zhuǎn)載者最起碼注明作者和出處!http://www.cnblogs.com/GuominQiu
//---------------------------------------------------------------------------
//判斷日期格式是否正確
//返回值是錯(cuò)誤信息, 無(wú)錯(cuò)誤信息即表示合法日期字符串
function isDateString(strDate){
var strSeparator = "-"; //日期分隔符
var strDateArray;
var intYear;
var intMonth;
var intDay;
var boolLeapYear;
var ErrorMsg = ""; //出錯(cuò)信息
strDateArray = strDate.split(strSeparator);
//沒(méi)有判斷長(zhǎng)度,其實(shí)2008-8-8也是合理的//strDate.length != 10 ||
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 += "日期格式錯(cuò)誤: 年月日必須為純數(shù)字";
return ErrorMsg;
}
if(intMonth>12 || intMonth<1) {
ErrorMsg += "日期格式錯(cuò)誤: 月份必須介于1和12之間";
return ErrorMsg;
}
if((intMonth==1||intMonth==3||intMonth==5||intMonth==7
||intMonth==8||intMonth==10||intMonth==12)
&&(intDay>31||intDay<1)) {
ErrorMsg += "日期格式錯(cuò)誤: 大月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)
&&(intDay>30||intDay<1)) {
ErrorMsg += "日期格式錯(cuò)誤: 小月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if(intMonth==2){
if(intDay < 1) {
ErrorMsg += "日期格式錯(cuò)誤: 日期必須大于或等于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 += "日期格式錯(cuò)誤: 閏年的2月份天數(shù)不能超過(guò)29";
return ErrorMsg;
}
} else {
if(intDay > 28) {
ErrorMsg += "日期格式錯(cuò)誤: 非閏年的2月份天數(shù)不能超過(guò)28";
return ErrorMsg;
}
}
}
return ErrorMsg;
}
復(fù)制代碼 代碼如下:
//---------------------------------------------------------------------------
//判斷日期格式是否正確
//返回值是錯(cuò)誤信息, 無(wú)錯(cuò)誤信息即表示合法日期字符串
function isDateString(strDate){
var strSeparator = "-"; //日期分隔符
var strDateArray;
var intYear;
var intMonth;
var intDay;
var boolLeapYear;
var ErrorMsg = ""; //出錯(cuò)信息
strDateArray = strDate.split(strSeparator);
//沒(méi)有判斷長(zhǎng)度,其實(shí)2008-8-8也是合理的//strDate.length != 10 ||
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 += "日期格式錯(cuò)誤: 年月日必須為純數(shù)字";
return ErrorMsg;
}
if(intMonth>12 || intMonth<1) {
ErrorMsg += "日期格式錯(cuò)誤: 月份必須介于1和12之間";
return ErrorMsg;
}
if((intMonth==1||intMonth==3||intMonth==5||intMonth==7
||intMonth==8||intMonth==10||intMonth==12)
&&(intDay>31||intDay<1)) {
ErrorMsg += "日期格式錯(cuò)誤: 大月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)
&&(intDay>30||intDay<1)) {
ErrorMsg += "日期格式錯(cuò)誤: 小月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if(intMonth==2){
if(intDay < 1) {
ErrorMsg += "日期格式錯(cuò)誤: 日期必須大于或等于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 += "日期格式錯(cuò)誤: 閏年的2月份天數(shù)不能超過(guò)29";
return ErrorMsg;
}
} else {
if(intDay > 28) {
ErrorMsg += "日期格式錯(cuò)誤: 非閏年的2月份天數(shù)不能超過(guò)28";
return ErrorMsg;
}
}
}
return ErrorMsg;
}
相關(guān)文章
JS date對(duì)象的減法處理實(shí)現(xiàn)代碼
JS date對(duì)象的減法處理實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-12-12
javascript 當(dāng)前日期轉(zhuǎn)化為中文的實(shí)現(xiàn)代碼
有時(shí)候需要將當(dāng)面的日期,轉(zhuǎn)換成中文,這里是js的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05
javascript實(shí)現(xiàn)的距離現(xiàn)在多長(zhǎng)時(shí)間后的一個(gè)格式化的日期
距離現(xiàn)在多長(zhǎng)時(shí)間后的一個(gè)格式化的日期,大家可以看看。2009-10-10
Javascript倒計(jì)時(shí)頁(yè)面跳轉(zhuǎn)實(shí)例小結(jié)
在js中實(shí)現(xiàn)頁(yè)面定時(shí)跳轉(zhuǎn)我們要使用setInterval或setTimeOut函數(shù),當(dāng)然還可以使用頁(yè)面的meta實(shí)現(xiàn)了,下面使用js實(shí)現(xiàn)的幾個(gè)代碼2013-09-09
用js實(shí)現(xiàn)頁(yè)面顯示當(dāng)前日期和時(shí)間的代碼
js時(shí)間日期顯示效果代碼2008-04-04
JavaScript 時(shí)分秒時(shí)間代碼(自動(dòng)補(bǔ)零)
JavaScript 時(shí)分秒時(shí)間代碼,時(shí)間小于10的就補(bǔ)充一個(gè)零。2010-02-02

