UNPKG

amos-tool

Version:

amos ui tool

148 lines (145 loc) 6.95 kB
"use strict"; var utils = require("./../utils"); function isValidDate(e) { return e instanceof Date && (e instanceof Date && !isNaN(e.getTime())); } module.exports = { formatDate: function(e, t) { var n = null == e ? new Date : e; if (utils.isNumber(n) && !utils.isString(n) && (n = Number(n)), n instanceof Date || (n = new Date(n)), !isValidDate(n)) return e; t = t || "yyyy-MM-dd HH:mm:ss"; var a = { y: n.getFullYear(), M: n.getMonth() + 1, d: n.getDate(), q: Math.floor((n.getMonth() + 3) / 3), w: n.getDay(), H: n.getHours(), h: n.getHours() % 12 == 0 ? 12 : n.getHours() % 12, m: n.getMinutes(), s: n.getSeconds(), S: n.getMilliseconds() }, r = [ "天", "一", "二", "三", "四", "五", "六" ], o = function(e) { t = t.replace(new RegExp(e + "+", "g"), function(t) { var n = a[e] + ""; if ("w" == e) return (t.length > 2 ? "星期" : "周") + r[n]; for (var o = 0, i = n.length; o < t.length - i; o++) n = "0" + n; return 1 == t.length ? n : n.substring(n.length - t.length); }); }; for (var i in a) o(i); return t; }, formatDate2: function(e, t) { if ("number" == typeof (e = null == e ? new Date : e) && (e = new Date(e)), "string" == typeof e) return e; var n = { "M+": e.getMonth() + 1, "d+": e.getDate(), "H+": e.getHours(), "m+": e.getMinutes(), "s+": e.getSeconds() }; for (var a in /(y+)/.test(t) && (t = t.replace(RegExp.$1, "".concat(e.getFullYear()).substr(4 - RegExp.$1.length))), n) new RegExp("(".concat(a, ")")).test(t) && (t = t.replace(RegExp.$1, 1 === RegExp.$1.length ? n[a] : "00".concat(n[a]).substr("".concat(n[a]).length))); return t; }, timetrans: function(e) { return (e = new Date(1e3 * e)).getFullYear() + "-" + ((e.getMonth() + 1 < 10 ? "0" + (e.getMonth() + 1) : e.getMonth() + 1) + "-") + ((e.getDate() < 10 ? "0" + e.getDate() : e.getDate()) + " ") + ((e.getHours() < 10 ? "0" + e.getHours() : e.getHours()) + ":") + ((e.getMinutes() < 10 ? "0" + e.getMinutes() : e.getMinutes()) + ":") + (e.getSeconds() < 10 ? "0" + e.getSeconds() : e.getSeconds()); }, formatDateToFriendly: function(e) { e = "number" == typeof (e = e || new Date) ? new Date(e) : e; var t = new Date; if (t.getTime() - e.getTime() < 6e4) return "刚刚"; var n = this.formatDate(e, "yyyy年M月d日"); return n == this.formatDate(t, "yyyy年M月d日") ? this.formatDate(e, "HH:mm") : e.getFullYear() == t.getFullYear() ? this.formatDate(e, "M月d日") : n; }, friendlyDate: function(e) { var t = +new Date - e, n = 6e4, a = 36e5, r = 864e5, o = 30 * r, i = 12 * o; return t >= i ? parseInt(t / i) + "年前" : t >= o ? parseInt(t / o) + "个月前" : t >= r ? parseInt(t / r) + "天前" : t >= a ? parseInt(t / a) + "小时前" : t >= n ? parseInt(t / n) + "分钟前" : t >= 1e3 ? "刚刚" : void 0; }, formatDurationToFriendly: function(e) { return e < 60 ? e + "秒" : e < 3600 ? (e - e % 60) / 60 + "分" + e % 60 + "秒" : e < 86400 ? (e - e % 3600) / 60 / 60 + "小时" + Math.round(e % 3600 / 60) + "分" : (e / 60 / 60 / 24).toFixed(1) + "天"; }, formatTimeToFriendly: function(e) { var t = Math.floor(e / 60), n = e % 60; return (t = t < 10 ? "0" + t : t) + ":" + (n = n < 10 ? "0" + n : n); }, isLeapYear: function(e) { return void 0 === e && (e = new Date), e instanceof Date && (e = e.getFullYear()), e % 4 == 0 && e % 100 != 0 || e % 400 == 0; }, getMonthDays: function(e, t) { var n, a; null == e && (e = new Date), e instanceof Date ? (n = e.getFullYear(), a = e.getMonth()) : "number" == typeof e && (n = e, a = t - 1); var r = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; return 1 == a && this.isLeapYear(n) ? r[a] + 1 : r[a]; }, countDays: function(e, t) { var n = "yyyy-MM-dd"; return e instanceof Date && t instanceof Date && (e = this.format(n, e), t = this.format(n, t)), "string" == typeof e && "string" == typeof t ? (e = this.parseDate(e, n), t = this.parseDate(t, n), (e.getTime() - t.getTime()) / 864e5) : (console.error("参数格式无效!"), 0); }, parseDate: function(e, t) { var n = { y: 0, M: 1, d: 0, H: 0, h: 0, m: 0, s: 0, S: 0 }; (t = t || "yyyy-MM-dd").replace(/([^yMdHmsS]*?)(([yMdHmsS])\3*)([^yMdHmsS]*?)/g, function(t, a, r, o, i, s, u) { return e = e.replace(new RegExp(a + "(\\d{" + r.length + "})" + i), function(e, t) { return n[o] = parseInt(t), ""; }), ""; }), n.M--; var a = new Date(n.y, n.M, n.d, n.H, n.m, n.s); return 0 !== n.S && a.setMilliseconds(n.S), a; }, getEndTime: function(e) { var t = new Date, n = new Date(e).getTime() - t.getTime(), a = 0, r = 0, o = 0, i = 0; return n >= 0 && (a = Math.floor(n / 1e3 / 3600 / 24), r = Math.floor(n / 1e3 / 60 / 60 % 24), o = Math.floor(n / 1e3 / 60 % 60), i = Math.floor(n / 1e3 % 60)), { d: a, h: r, m: o, s: i }; }, getEndTimeStr: function(e) { var t = this.getEndTime(e); return "剩余时间" + t.d + "天 " + t.h + "小时 " + t.m + " 分钟" + t.s + " 秒"; }, getMonthOfDay: function(e) { var t = new Date(e), n = t.getFullYear(), a = t.getMonth() + 1; return 2 == a ? n % 4 == 0 && n % 100 == 0 && n % 400 == 0 || n % 4 == 0 && n % 100 != 0 ? 28 : 29 : 1 == a || 3 == a || 5 == a || 7 == a || 8 == a || 10 == a || 12 == a ? 31 : 30; }, getYearOfDay: function(e) { var t = this.getFirstDayOfYear(e), n = this.getLastDayOfYear(e), a = (new Date(n).getTime() - new Date(t).getTime()) / 1e3; return Math.ceil(a / 86400); }, getFirstDayOfYear: function(e) { return new Date(e).getFullYear() + "-01-01 00:00:00"; }, getLastDayOfYear: function(e) { var t = new Date(e).getFullYear(), n = t + "-12-01 00:00:00"; return t + "-12-" + this.getMonthOfDay(n) + " 23:59:59"; }, getDayOfYear: function(e) { var t = this.getFirstDayOfYear(e), n = (new Date(e).getTime() - new Date(t).getTime()) / 1e3; return Math.ceil(n / 86400); }, getDayOfYearWeek: function(e) { var t = this.getDayOfYear(e); return Math.ceil(t / 7); }, timeFn: function(e) { var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 1, n = new Date(e.replace(/-/g, "/")), a = (new Date).getTime() - n.getTime(), r = Math.floor(a / 864e5), o = a % 864e5, i = Math.floor(o / 36e5), s = o % 36e5, u = Math.floor(s / 6e4), g = s % 6e4, c = Math.round(g / 1e3); return t ? "相差".concat(r, "天").concat(i, "小时").concat(u, "分钟").concat(c, "秒") : "".concat(a, "时间差的毫秒数,").concat(r, "计算出相差天数,").concat(o, "计算天数后剩余的毫秒数,").concat(i, "计算出小时数,").concat(u, "计算相差分钟数,").concat(c, "计算相差秒数"); } };