1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| /** * 日期时间工具类 * @type {{dateFormat}} */ var DateTime = function () { var patterns = { PATTERN_ERA: 'G', // Era 标志符 Era strings. For example: "AD" and "BC" PATTERN_YEAR: 'y', // 年 PATTERN_MONTH: 'M', // 月份 PATTERN_DAY_OF_MONTH: 'd', // 月份的天数 PATTERN_HOUR_OF_DAY1: 'k', // 一天中的小时数(1-24) PATTERN_HOUR_OF_DAY0: 'H', // 24 小时制,一天中的小时数(0-23) PATTERN_MINUTE: 'm', // 小时中的分钟数 PATTERN_SECOND: 's', // 秒 PATTERN_MILLISECOND: 'S', // 毫秒 PATTERN_DAY_OF_WEEK: 'E', // 一周中对应的星期,如星期一,周一 PATTERN_DAY_OF_YEAR: 'D', // 一年中的第几天 PATTERN_DAY_OF_WEEK_IN_MONTH: 'F', // 一月中的第几个星期(会把这个月总共过的天数除以7,不够准确,推荐用W) PATTERN_WEEK_OF_YEAR: 'w', // 一年中的第几个星期 PATTERN_WEEK_OF_MONTH: 'W', // 一月中的第几星期(会根据实际情况来算) PATTERN_AM_PM: 'a', // 上下午标识 PATTERN_HOUR1: 'h', // 12 小时制 ,am/pm 中的小时数(1-12) PATTERN_HOUR0: 'K', // 和 h 类型 PATTERN_ZONE_NAME: 'z', // 时区名 PATTERN_ZONE_VALUE: 'Z', // 时区值 PATTERN_WEEK_YEAR: 'Y', // 和 y 类型 PATTERN_ISO_DAY_OF_WEEK: 'u', PATTERN_ISO_ZONE: 'X' };
var week = { 'ch': { "0": "\u65e5", "1": "\u4e00", "2": "\u4e8c", "3": "\u4e09", "4": "\u56db", "5": "\u4e94", "6": "\u516d" }, 'en': { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday" } };
/** * 获取当前时间 * @returns {string} */ var handlerGetCurrentTime = function () { var today = new Date(); var year = today.getFullYear(); var month = today.getMonth() + 1; var day = today.getDate(); var hours = today.getHours(); var minutes = today.getMinutes(); var seconds = today.getSeconds(); var timeString = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; return timeString; };
/** * 比较时间大小 * time1 > time2 return 1 * time1 < time2 return -1 * time1 == time2 return 0 * @param time1 * @param time2 * @returns {number} */ var handlerCompareTime = function (time1, time2) { if (Date.parse(time1.replace(/-/g, "/")) > Date.parse(time2.replace(/-/g, "/"))) { return 1; } else if (Date.parse(time1.replace(/-/g, "/")) < Date.parse(time2.replace(/-/g, "/"))) { return -1; } else if (Date.parse(time1.replace(/-/g, "/")) == Date.parse(time2.replace(/-/g, "/"))) { return 0; } };
/** * 是否闰年 * @param year * @returns {boolean} */ var handlerIsLeapYear = function (year) { return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0); };
/** * 获取某个月的天数,从 0 开始 * @param year * @param month * @returns {*} */ var handlerGetDaysOfMonth = function (year, month) { return [31, (this.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; };
/** * 获取某个月的天数,从 0 开始 * @param year * @param month * @returns {number} */ var handlerGetDaysOfMonth2 = function (year, month) { // 将天置为 0,会获取其上个月的最后一天 month = parseInt(month) + 1; var date = new Date(year, month, 0); return date.getDate(); };
/** * 距离现在几天的日期:负数表示今天之前的日期,0 表示今天,整数表示未来的日期 * 如 -1 表示昨天的日期,0 表示今天,2 表示后天 * @param days * @returns {string} */ var handlerFromToday = function (days) { var today = new Date(); today.setDate(today.getDate() + days); var date = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + today.getDate(); return date; };
/** * 格式化日期时间 * @param dateTime 需要格式化的日期时间 * @param pattern 格式化的模式,如 yyyy-MM-dd hh(HH):mm:ss.S a k K E D F w W z Z * @returns {*} */ var handlerFormat = function (dateTime, pattern) { var date = new Date(dateTime); if (pattern == null || pattern.length == 0) { return date.toLocaleString(); } return pattern.replace(/([a-z])\1*/ig, function (matchStr, group1) { var replacement = ""; switch (group1) { case patterns.PATTERN_ERA: //G break; case patterns.PATTERN_WEEK_YEAR: //Y case patterns.PATTERN_YEAR: //y replacement = date.getFullYear(); break; case patterns.PATTERN_MONTH: //M var month = date.getMonth() + 1; replacement = (month < 10 && matchStr.length >= 2) ? "0" + month : month; break; case patterns.PATTERN_DAY_OF_MONTH: //d var days = date.getDate(); replacement = (days < 10 && matchStr.length >= 2) ? "0" + days : days; break; case patterns.PATTERN_HOUR_OF_DAY1: //k(1~24) var hours24 = date.getHours(); replacement = hours24; break; case patterns.PATTERN_HOUR_OF_DAY0: //H(0~23) var hours24 = date.getHours(); replacement = (hours24 < 10 && matchStr.length >= 2) ? "0" + hours24 : hours24; break; case patterns.PATTERN_MINUTE: //m var minutes = date.getMinutes(); replacement = (minutes < 10 && matchStr.length >= 2) ? "0" + minutes : minutes; break; case patterns.PATTERN_SECOND: //s var seconds = date.getSeconds(); replacement = (seconds < 10 && matchStr.length >= 2) ? "0" + seconds : seconds; break; case patterns.PATTERN_MILLISECOND: //S var milliSeconds = date.getMilliseconds(); replacement = milliSeconds; break; case patterns.PATTERN_DAY_OF_WEEK: //E var day = date.getDay(); replacement = week['ch'][day]; break; case patterns.PATTERN_DAY_OF_YEAR: //D replacement = dayOfTheYear(date); break; case patterns.PATTERN_DAY_OF_WEEK_IN_MONTH: //F var days = date.getDate(); replacement = Math.floor(days / 7); break; case patterns.PATTERN_WEEK_OF_YEAR: //w var days = dayOfTheYear(date); replacement = Math.ceil(days / 7); break; case patterns.PATTERN_WEEK_OF_MONTH: //W var days = date.getDate(); replacement = Math.ceil(days / 7); break; case patterns.PATTERN_AM_PM: //a var hours24 = date.getHours(); replacement = hours24 < 12 ? "\u4e0a\u5348" : "\u4e0b\u5348"; break; case patterns.PATTERN_HOUR1: //h(1~12) var hours12 = date.getHours() % 12 || 12; //0转为12 replacement = (hours12 < 10 && matchStr.length >= 2) ? "0" + hours12 : hours12; break; case patterns.PATTERN_HOUR0: //K(0~11) var hours12 = date.getHours() % 12; replacement = hours12; break; case patterns.PATTERN_ZONE_NAME: //z replacement = handlerGetZoneNameValue(date)['name']; break; case patterns.PATTERN_ZONE_VALUE: //Z replacement = handlerGetZoneNameValue(date)['value']; break; case patterns.PATTERN_ISO_DAY_OF_WEEK: //u break; case patterns.PATTERN_ISO_ZONE: //X break; default: break; } return replacement; }); };
/** * 计算一个日期是当年的第几天 * @param date * @returns {number} */ var handlerDayOfTheYear = function (date) { var obj = new Date(date); var year = obj.getFullYear(); var month = obj.getMonth(); //从0开始 var days = obj.getDate(); var daysArr = [31, (this.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; for (var i = 0; i < month; i++) { days += daysArr[i]; } return days; };
/** * 获得时区名和值 * @param dateObj * @returns {{name: string, value: string}} */ var handlerGetZoneNameValue = function (dateObj) { var date = new Date(dateObj); date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())); var arr = date.toString().match(/([A-Z]+)([-+]\d+:?\d+)/); var obj = { 'name': arr[1], 'value': arr[2] }; return obj; };
return { getCurrentTime: function () { return handlerGetCurrentTime(); },
compareTime: function (time1, time2) { return handlerCompareTime(time1, time2); },
isLeapYear: function (year) { return handlerIsLeapYear(year); },
getDaysOfMonth: function (year, month) { return handlerGetDaysOfMonth(year, month); },
getDaysOfMonth2: function (year, month) { return handlerGetDaysOfMonth2(year, month); },
fromToday: function (days) { return handlerFromToday(days); },
format: function (dateTime, pattern) { return handlerFormat(dateTime, pattern); },
dayOfTheYear: function (date) { return handlerDayOfTheYear(date); },
getZoneNameValue: function (dateObj) { return handlerGetZoneNameValue(dateObj); } } }();
|