日期:2014-05-16 浏览次数:20400 次
//计算两个日期相差几天几小时 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 29 else 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();