1 /* 2 *javascript 中 Date的工具类,方便string格式的与js时间互转 3 *主要方法:addDays,addMonths,addYears 4 */ 5 function runtime(datetime) { 6 this._millsec = 0; 7 var _datetime = null; 8 switch (typeof datetime) { 9 case 'string': 10 _datetime = this.strToDate(datetime); 11 break; 12 case 'number': 13 case 'object': 14 _datetime = new Date(datetime); 15 break; 16 default: 17 _datetime = new Date(); 18 break; 19 } 20 this._millsec = _datetime.getTime(); 21 this._synFromMillSec(); 22 } 23 runtime.prototype._synFromMillSec = function () { 24 var datetime = new Date(this._millsec); 25 this.dateTime = datetime; 26 this.month = datetime.getMonth(); 27 this.year = datetime.getFullYear(); 28 this.dayOfMonth = datetime.getDate(); 29 this.dayOfWeek = datetime.getDay(); 30 this.hours = datetime.getHours(); 31 this.mins = datetime.getMinutes(); 32 this.seconds = datetime.getSeconds(); 33 this.milliSeconds = datetime.getMilliseconds(); 34 this.totalMilliSeconds = this._millsec; 35 } 36 37 runtime.prototype._synFromNumber = function () { 38 var datetime = new Date(this.year, this.month, this.dayOfMonth, this.hour, this.mins, this.seconds, this.milliSeconds); 39 this._millsec = datetime.getMilliseconds(); 40 this.dateTime = datetime; 41 this.totalMilliSeconds = this._millsec; 42 } 43 44 //当前时间加毫秒 返回当前实例 [整数] 45 runtime.prototype.addMilliSeconds = function (ms) { 46 this._millsec += ms; 47 this._synFromMillSec(); 48 return this; 49 } 50 //当前时间加秒 返回当前实例 [整数] 51 runtime.prototype.addSeconds = function (sec) { 52 return this.addMilliSeconds(sec * 1000); 53 } 54 //当前时间加分钟 返回当前实例 [整数] 55 runtime.prototype.addMins = function (mins) { 56 return this.addMilliSeconds(mins * 60 * 1000); 57 } 58 //当前时间加小时 返回当前实例 [整数] 59 runtime.prototype.addHours = function (hours) { 60 return this.addMilliSeconds(hours * 60 * 60 * 1000); 61 } 62 //当前时间加天 返回当前实例 [整数] 63 runtime.prototype.addDays = function (days) { 64 return this.addMilliSeconds(days * 24 * 60 * 60 * 1000); 65 } 66 //当前时间加周 返回当前实例 [整数] 67 runtime.prototype.addWeeks = function (weeks) { 68 return this.addMilliSeconds(weeks * 7 * 24 * 60 * 60 * 1000); 69 } 70 71 //当前时间加月 返回当前实例 [整数] 72 runtime.prototype.addMonths = function (months) { 73 var tm = months % 12; 74 var ty = (months - tm) / 12; 75 var cm = this.month + tm; 76 if (cm > 11) { 77 ty++; 78 cm = cm - 11; 79 } else if (cm < 0) { 80 ty--; 81 cm = cm + 11; 82 } 83 this.year += ty; 84 this.month = cm; 85 var currMDays = this.monthDays(); 86 if (currMDays < this.dayOfMonth) { 87 this.dayOfMonth = currMDays; 88 } 89 this._synFromNumber(); 90 return this; 91 } 92 //当前时间加年 返回当前实例 [整数] 93 runtime.prototype.addYears = function (years) { 94 return this.addMonths(years * 12); 95 } 96 97 //是否是润年 98 runtime.prototype.isRun = function () { 99 var pYear = this.year; 100 if ((pYear % 4 == 0 && pYear % 100 != 0) || (pYear % 100 == 0 && pYear % 400 == 0)) { 101 return true; 102 } else { 103 return false; 104 } 105 } 106 //当月天数 107 runtime.prototype.monthDays = function () { 108 switch (this.month + 1) { 109 case 1: 110 case 3: 111 case 5: 112 case 7: 113 case 8: 114 case 10: 115 case 12: 116 return 31; 117 case 2: 118 if (this.isRun()) 119 return 29; 120 return 28; 121 default: 122 return 30; 123 } 124 } 125 //中文星期 126 runtime.prototype.weekToCnName = function () { 127 var show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'); 128 return show_day[this.dayOfWeek]; 129 } 130 131 //string 转 Date 如:2012-12-12 01:01:01 132 runtime.prototype.strToDate = function (str) { 133 if (!str) 134 return; 135 var s = str + ''; 136 var sp = ''; 137 if (s.indexOf('-') > 0) { 138 sp = '-'; 139 } else if (s.indexOf('/') > 0) { 140 sp = '/'; 141 } 142 else { 143 return; 144 } 145 var year = s.match(new RegExp('[0-9]{4}'))[0]; 146 var month = s.match(new RegExp(sp + '[0-9]{1,2}', 'g'))[0].replace(sp + "", ''); 147 var day = s.match(new RegExp(sp + '[0-9]{1,2}', 'g'))[1].replace(sp + "", ''); 148 var contain_time = s.indexOf(':') > 0; 149 var hour = 0, min = 0, second = 0, ms = 0; 150 if (contain_time) { 151 hour = s.match(new RegExp('[0-9]{2}:', 'g'))[0].replace(/:/g, ''); 152 min = s.match(new RegExp('[0-9]{2}:', 'g'))[1].replace(/:/g, ''); 153 second = s.match(new RegExp(':[0-9]{2}', 'g'))[1].replace(/:/g, ''); 154 ms = s.match(new RegExp('.[0-9]{3}$', 'g'))[0].replace(/./g, ''); 155 hour = parseInt(hour); 156 min = parseInt(min); 157 second = parseInt(second); 158 if (ms) 159 ms = parseInt(ms); 160 else 161 ms = 0; 162 } 163 var dt = new Date(parseInt(year), parseInt(month) - 1, parseInt(day), hour, min, second, ms); 164 return dt; 165 } 166 // 深copy当前对象 167 runtime.prototype.clone = function () { 168 return new runtime(this.dateTime); 169 } 170 171 //转字符串输入 如 fmt=yyyy年MM月dd日cweek 172 runtime.prototype.formatDateString = function (fmt) { 173 var result = fmt || ""; 174 result = result.replace(/yyyy/g, this.year) 175 .replace(/yy/g, this.year.toString().substr(2, 2)) 176 .replace(/MM/g, (this.month + 1) < 10 ? ('0' + (this.month + 1).toString()) : (this.month + 1)) 177 .replace(/M/g, (this.month + 1).toString()) 178 .replace(/dd/g, this.dayOfMonth < 10 ? ('0' + this.dayOfMonth) : this.dayOfMonth) 179 .replace(/d/g, this.dayOfMonth) 180 .replace(/HH/g, this.hours < 10 ? ('0' + this.hours) : this.hours) 181 .replace(/H/g, this.hours) 182 .replace(/mm/g, this.mins < 10 ? ('0' + this.mins) : this.mins) 183 .replace(/m/g, this.mins) 184 .replace(/ss/g, this.seconds < 10 ? ('0' + this.seconds) : this.seconds) 185 .replace(/s/g, this.seconds) 186 .replace(/fff/g, this.milliSeconds < 10 ? ('00' + this.milliSeconds) : (this.milliSeconds < 100 ? ('0' + this.milliSeconds) : (this.milliSeconds))) 187 .replace(/ff/g, this.milliSeconds) 188 .replace(/f/g, this.milliSeconds) 189 .replace(/cweek/g, this.weekToCnName()); 190 return result; 191 } 192 193 //转为string 194 runtime.prototype.toString = function () { 195 return this.formatDateString('yyyy/MM/dd HH:mm:ss.fff'); 196 }