UNPKG

@buession/prototype

Version:

A native object extension framework for Javascript.

230 lines 7.38 kB
"use strict"; /** * Date 对象扩展 */ /** * 判断是否为闰年 * * @return boolean */ Date.prototype.isLeapYear = function () { var year = this.getFullYear(); return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); }; /** * 获取季节 * * @return 季节 */ Date.prototype.getSeason = function () { var month = this.getMonth(); if (month >= 3 && month <= 5) { return 0; } else if (month >= 6 && month <= 8) { return 1; } else if (month >= 9 && month <= 11) { return 2; } else if (month >= 12 || month <= 2) { return 3; } else { return 0; } }; /** * 获取年份中的第几天 * * @return 年份中的第几天 */ Date.prototype.getDayOfYear = function () { var month_days = this.isLeapYear() == true ? [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var days = this.getDate(); for (var m = 0, month = this.getMonth(); m < month; m++) { days += month_days[m]; } return days; }; /** * 获取年份总天数 * * @return 年份总天数 */ Date.prototype.getDaysOfYear = function () { return this.isLeapYear() ? 366 : 365; }; /** * Format a date object into a string value. * @param format string - the desired format of the date * * The format can be combinations of the following: * * y - 年 * n - 季度(1 到 4) * N - 季度名称 * A - 季度中文名称 * M - 月 * f - 月(Jan 到 Dec) * F - 月(January 到 December) * C - 月,中文名称 * d - 日 * Y - 年份中的第几天(0 到 365) * T - 月份有几天(28 到 30) * j - 每月天数后面的英文后缀(st,nd,rd 或者 th) * e - 星期几,数字表示,0(表示星期天)到 6(表示星期六) * E - 星期几,数字表示,1(表示星期一)到 7(表示星期天) * l - 星期几,文本表示,3 个字母(Mon 到 Sun) * L - 星期几,完整的文本格式(Sunday 到 Saturday) * w - 星期几,中文名称 * W - 一月中第几个星期几 * i - 月份中的第几周 * o - 年份中的第几周 * h - 小时(1~12) * H - 小时(0~23) * m - 分 * s - 秒 * S - 毫秒 * a - 上午/下午标记 * O - 与格林威治时间相差的小时数 * P - 与格林威治时间相差的小时数,小时和分钟之间有冒号分隔 * Z - 时区 * * @return 格式化后的日期时间 */ Date.prototype.format = function (format) { if (Object.isString(format) === false) { throw "Invalid argument format"; } var $this = this; var _season_map = { "N": ["Spring", "Summer", "Autumn", "Winter"], "A": ["\u6625", "\u590f", "\u79cb", "\u51ac"] }; var _month_map = { "f": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], "F": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], "C": ["\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D", "\u4E03", "\u516B", "\u4E5D", "\u5341", "\u5341\u4E00", "\u5341\u4E8C"] }; var _weekday_map = { "W": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], "WW": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], "WC": ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"] }; var season = -1; var seasonFn = function () { return Math.floor(($this.getMonth() + 3) / 3); }; var $funcs = { // 年 "y": function (pattern) { return ($this.getFullYear() + "").substring(4 - pattern.length); }, // 季度(1 到 4) "n": function () { if (season === -1) { season = seasonFn(); } return season; }, // 季度名称 "N": function () { if (season === -1) { season = seasonFn(); } return _season_map["N"][season - 1]; }, // 季度中文名称 "A": function () { if (season === -1) { season = seasonFn(); } return _season_map["A"][season - 1]; }, // 月 "M": function (pattern) { var $month = $this.getMonth() + 1; var result = $month < 10 ? "0" + $month : "" + $month; return result.substring(2 - pattern.length); }, // 月(Jan 到 Dec) "f": function () { var $month = $this.getMonth(); return _month_map["f"][$month]; }, // 月(January 到 December) "F": function () { var $month = $this.getMonth(); return _month_map["F"][$month]; }, // 月,中文名称 "C": function () { var $month = $this.getMonth(); return _month_map["C"][$month]; }, // 星期数字,0 到 6 表示 "e": function () { return $this.getDay(); }, // 星期数字,1 到 7 表示 "E": function () { return $this.getDay() + 1; }, // 星期英文缩写 "l": function () { var $weekday = $this.getDay(); return _weekday_map["W"][$weekday]; }, // 星期英文全称 "L": function () { var $weekday = $this.getDay(); return _weekday_map["WC"][$weekday]; }, // 星期中文名称 "w": function () { var $weekday = $this.getDay(); return _weekday_map["WC"][$weekday]; }, // 日 "d": function (pattern) { var $date = $this.getDate(); var result = $date < 10 ? "0" + $date : "" + $date; return result.substring(2 - pattern.length); }, // 小时 "h": function (pattern) { var $hour = $this.getHours(); var result = $hour % 12 === 0 ? "12" : $hour % 12; result = $hour < 10 ? "0" + $hour : "" + $hour; return result.substring(2 - pattern.length); }, // 小时 "H": function (pattern) { var $hour = $this.getHours(); var result = $hour < 10 ? "0" + $hour : "" + $hour; return result.substring(2 - pattern.length); }, // 分钟 "m": function (pattern) { var $minutes = $this.getMinutes(); var result = $minutes < 10 ? "0" + $minutes : "" + $minutes; return result.substring(2 - pattern.length); }, // 秒钟 "s": function (pattern) { var $seconds = $this.getSeconds(); var result = $seconds < 10 ? "0" + $seconds : "" + $seconds; return result.substring(2 - pattern.length); }, // 毫秒 "S": function (pattern) { var $mise = $this.getMilliseconds(); var result = $mise < 10 ? "0" + $mise : "" + $mise; return result.substring(2 - pattern.length); } }; return format.replace(/([ynNAMfFCdYTjeElLwWiohHmsSaOPZ])+/g, function (all, t) { var fn = $funcs[t]; return Object.isFunction(fn) === true ? fn(all) : all; }); }; //# sourceMappingURL=date.js.map