jr-util
Version:
这是MJR的JS工具库。定期更新
50 lines (49 loc) • 2.14 kB
JavaScript
function d(t) {
("" + t).length === 10 ? t = parseInt(t) * 1e3 : t = +t;
const e = new Date(t), u = (Date.now() - e) / 1e3;
return u < 30 ? "\u521A\u521A" : u < 3600 ? Math.ceil(u / 60) + "\u5206\u949F\u524D" : u < 3600 * 24 ? Math.ceil(u / 3600) + "\u5C0F\u65F6\u524D" : "1\u5929\u524D";
}
function F(t) {
let e;
t || (t = new Date().getTime()), `${t}`.length === 10 ? e = new Date(t * 1e3) : e = new Date(t);
let i = e.getFullYear().toString().padStart(4, "0"), u = (e.getMonth() + 1).toString().padStart(2, "0"), a = e.getDate().toString().padStart(2, "0"), l = e.getHours().toString().padStart(2, "0"), n = e.getMinutes().toString().padStart(2, "0"), r = e.getSeconds().toString().padStart(2, "0");
return {
year: i,
month: u,
day: a,
hour: l,
minute: n,
second: r
};
}
function s(t) {
if (!(t.length === 10 || t.length === 19))
throw new Error("Function:dateToTimestamp \u8F93\u5165\u65F6\u95F4\u683C\u5F0F\u6709\u8BEF");
let e;
return t.length === 10 ? e = new Date(t + " 00:00:00") : e = new Date(t), e.getTime() / 1e3;
}
function f(t, e, i = "tb") {
if (t.length !== e.length)
throw new Error("Function:dateTb \u5F00\u59CB\u65F6\u95F4\u548C\u7ED3\u675F\u65F6\u95F4\u683C\u5F0F\u4E0D\u540C");
if (!(t.length === 10 || t.length === 19))
throw new Error("Function:dateToTimestamp \u8F93\u5165\u65F6\u95F4\u683C\u5F0F\u6709\u8BEF");
let u = s(t), a = s(e);
if (u > a)
throw new Error("Function:dateToTimestamp \u5F00\u59CB\u65F6\u95F4\u5927\u4E8E\u7ED3\u675F\u65F6\u95F4");
let l = Math.floor((a - u) / 3600 / 24), n, r;
i === "tb" ? (n = F(a), r = `${parseInt(n.year) - 1}-${n.month}-${n.day} ${n.hour}:${n.minute}:${n.second}`) : (n = F(u), r = `${n.year}-${n.month}-${n.day} ${n.hour}:${n.minute}:${n.second}`);
const g = s(r), o = F(g - l * 24 * 3600), h = `${o.year}-${o.month}-${o.day} ${o.hour}:${o.minute}:${o.second}`;
return t.length === 19 ? {
start: h,
end: r
} : {
start: h.substring(0, 10),
end: r.substring(0, 10)
};
}
export {
f as dateTHb,
s as dateToTimestamp,
d as formatTimeByNear,
F as timestampToTime
};