Javascript Date 常用處理函數

來源:互聯網
上載者:User
//計算兩個日期相差幾天幾小時Date.diffDayHourString = function(date1,date2){var date3;if(typeof date1 == "object"){date3=date2.getTime()-date1.getTime();  //時間差的毫秒數}else{date3=date2-date1;  //時間差的毫秒數}var days=Math.floor(date3/(24*3600*1000));var leave1=date3%(24*3600*1000);    //計算天數後剩餘的毫秒數var hours=Math.floor(leave1/(3600*1000));return days+"D "+hours+"H";}//計算兩個日期相差幾個小時Date.diffHourInt = function(date1,date2){var date3;if(typeof date1 == "object"){date3=date2.getTime()-date1.getTime();  //時間差的毫秒數}else{date3=date2-date1;  //時間差的毫秒數}var days=Math.floor(date3/(24*3600*1000));var leave1=date3%(24*3600*1000);    //計算天數後剩餘的毫秒數var hours=Math.floor(leave1/(3600*1000));return days*24+hours;}Date.getTime = function(dateStr/*yyyy-MM-dd HH:mm:ss*/){var d = new Date(dateStr);return d.getTime();}Date.MONTH_DAYS = [31,0,31,30,31,30,31,31,30,31,30,31];Date.ENG_MONTH_MAP = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6, 'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12};Date.MONTH_ENG_MAP = ['Jan','Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];//計算某年某月天數Date.getMonthDays = function(year,month){year-=0;month-=0;if(month==1){if (0==year%4&&((year%100!=0)||(year%400==0)))return 29else return 28;}else return Date.MONTH_DAYS[month];}/** * 給定年和周,算出這周的開始日期和結束日期 * 先檢查一年的開始時間是否是周一 * 然後計算出一年開始到某周的天數 * 開始日期 = 某年第一天毫秒數 + 天數的毫秒數 * 需要注意某一年的開始第一周和某年結束一周需要特殊處理 * */Date.getStartEndDateOfWeek = function(year,numOfWeek,outStr){year-=0;numOfWeek-=1;var b = new Date(year,0,1),bDayOfWeek = b.getDay()||7,startDate = new Date(),endDate = new Date(),extraDays = (7-bDayOfWeek+1),fullDays;if(extraDays!=7) {numOfWeek -= 1; fullDays = numOfWeek*7+extraDays;}else{fullDays = numOfWeek*7;}startDate.setTime(new Date(year,0,1).getTime() + (fullDays <= 0 ? 0 : fullDays*24*60*60*1000));endDate.setTime(startDate.getTime() + (numOfWeek < 0 && extraDays != 7 ? extraDays-1 : 6 )*24*60*60*1000);if(endDate.getFullYear() > year){endDate = new Date(year,11,31);}//console.log(startDate.toString(),endDate.toString());if(outStr){return (startDate.getMonth()+1)+"."+startDate.getDate()+"~"+(endDate.getMonth()+1)+"."+endDate.getDate();}else{return [startDate,endDate];}}//日期格式化Date.prototype.format = function (formatStr) {   var date = this;   /*   函數:填充0字元   參數:value-需要填充的字串, length-總長度   返回:填充後的字串   */  var zeroize = function (value, length) {   if (!length) {   length = 2;   }   value = new String(value);   for (var i = 0, zeros = ''; i < (length - value.length); i++) {   zeros += '0';   }   return zeros + value;   };   return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0) {   switch ($0) {   case 'd': return date.getDate();   case 'dd': return zeroize(date.getDate());   case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][date.getDay()];   case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];   case 'M': return date.getMonth() + 1;   case 'MM': return zeroize(date.getMonth() + 1);   case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()];   case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][date.getMonth()];   case 'yy': return new String(date.getFullYear()).substr(2);   case 'yyyy': return date.getFullYear();   case 'h': return date.getHours() % 12 || 12;   case 'hh': return zeroize(date.getHours() % 12 || 12);   case 'H': return date.getHours();   case 'HH': return zeroize(date.getHours());   case 'm': return date.getMinutes();   case 'mm': return zeroize(date.getMinutes());   case 's': return date.getSeconds();   case 'ss': return zeroize(date.getSeconds());   case 'l': return date.getMilliseconds();   case 'll': return zeroize(date.getMilliseconds());   case 'tt': return date.getHours() < 12 ? 'am' : 'pm';   case 'TT': return date.getHours() < 12 ? 'AM' : 'PM';   }   });   }//計算日期處在月中的第幾周Date.prototype.getMonthWeek = function () {        var date = this, w = date.getDay(), d = date.getDate();        return Math.ceil((d + 6 - w) / 7);    };    //計算日期處在月中的第幾周Date.prototype.getYearWeek = function () {           var date1 = this, date2 = new Date(date1.getFullYear(), 0, 1),            d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);        return Math.ceil((d + (((date2.getDay()||7) + 1) - 1)) / 7);    };    
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.