幾個常用的日期函數

來源:互聯網
上載者:User
標題     幾個常用的日期函數    chenzengxi(原作)
關鍵字     日期


        /* -- 計算兩個字元(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];
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.