CSDN博客

img chenzengxi

几个常用的日期函数

发表于2002/4/27 15:53:00  566人阅读

        /* -- 计算两个字符(YYYY-MM-DD)的日期相隔天数 -- */
function Date_Compare(asStartDate,asEndDate){
 var miStart = Date.parse(asStartDate.replace(//-/g, '/'));
 var miEnd   = Date.parse(asEndDate.replace(//-/g, '/'));
 return (miEnd-miStart)/(1000*24*3600);
}

      /*-- 判断一个字符串(YYYY-MM-DD)的日期是否正确 -- */
function Date_istrue(asDate){
 var lsDate  = asDate + "";
 var loDate  = lsDate.split("-");
 if (loDate.length!=3) return false;
 var liYear  = parseInt(loDate[0]);
 var liMonth = parseInt(loDate[1]);
 var liDay   = parseInt(loDate[2]);
 if ((loDate[0].length>4)||(loDate[1].length>2)||(loDate[2].length>2)) return false;
 if (isNaN(liYear)||isNaN(liMonth)||isNaN(liDay)) return false;
 if ((liYear<1900)||(liYear>3000)) return false;
 if ((liMonth>12)||(liMonth<=0))   return false;
 if (Date_getDay(liYear,liMonth)<liDay) return false;
 return !isNaN(Date.UTC(liYear,liMonth,liDay));
}

      /*-- 返回某年某月的天数-- */
function Date_getDay(aiYear,aiMonth){
 var loDay = [0,31,28,31,30,31,30,31,31,30,31,30,31];
 if (aiYear%4==0) loDay[2] = 29;
 return loDay[aiMonth];
}

0 0

相关博文

我的热门文章

img
取 消
img