UNPKG

jr-util

Version:

这是MJR的JS工具库。定期更新

2 lines (1 loc) 1.9 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});function h(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 l(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"),F=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:F,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 F=Math.floor((a-u)/3600/24),n,r;i==="tb"?(n=l(a),r=`${parseInt(n.year)-1}-${n.month}-${n.day} ${n.hour}:${n.minute}:${n.second}`):(n=l(u),r=`${n.year}-${n.month}-${n.day} ${n.hour}:${n.minute}:${n.second}`);const g=s(r),o=l(g-F*24*3600),d=`${o.year}-${o.month}-${o.day} ${o.hour}:${o.minute}:${o.second}`;return t.length===19?{start:d,end:r}:{start:d.substring(0,10),end:r.substring(0,10)}}exports.dateTHb=f;exports.dateToTimestamp=s;exports.formatTimeByNear=h;exports.timestampToTime=l;