index.js 1.9 KB

1
  1. "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.parse=exports.monthDiff=exports.format=exports.diff=void 0;var parse=exports.parse=function(e){return e instanceof Date?e:!isNaN(e)||/^\d+$/.test(e)?new Date(parseInt(e,10)):(e=(e=(e=(e=(e=(e=e.trim()).replace(/\.\d+/,"")).replace(/-/,"/").replace(/-/,"/")).replace(/T/," ").replace(/Z/," UTC")).replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2")).replace(/([\+\-]\d\d)$/," $100"),new Date(e))},monthDiff=exports.monthDiff=function(e,t){e=parse(e),t=parse(t);return 12*(e.getFullYear()-t.getFullYear())+e.getMonth()-t.getMonth()},diff=exports.diff=function(e,t,r){var e=parse(e),t=t?parse(t):new Date,a=t.getTime()-e.getTime();return"second"===r&&a/1e3||"minute"===r&&a/1e3/60||"hour"===r&&a/1e3/60/60||"day"===r&&a/1e3/60/60/24||"week"===r&&a/1e3/60/60/24/7||"month"===r&&monthDiff(e,t)||"quarter"===r&&monthDiff(e,t)/3||"year"===r&&monthDiff(e,t)/12||a},defaults={second:["刚刚","片刻后"],seconds:["%d 秒前","%d 秒后"],minute:["大约 1 分钟前","大约 1 分钟后"],minutes:["%d 分钟前","%d 分钟后"],hour:["大约 1 小时前","大约 1 小时后"],hours:["%d 小时前","%d 小时后"],day:["1 天前","1 天后"],days:["%d 天前","%d 天后"],month:["大约 1 个月前","大约 1 个月后"],months:["%d 月前","%d 月后"],year:["大约 1 年前","大约 1 年后"],years:["%d 年前","%d 年后"]},format=exports.format=function(e,t){function r(e,t){return e.replace(/%d/i,t)}var t=Object.assign({},defaults,t),a=e<0?1:0,e=Math.abs(e)/1e3,n=e/60,s=n/60,o=s/24,d=o/365;return e<10&&r(t.second[a],parseInt(e))||e<45&&r(t.seconds[a],parseInt(e))||e<90&&r(t.minute[a],1)||n<45&&r(t.minutes[a],parseInt(n))||n<90&&r(t.hour[a],1)||s<24&&r(t.hours[a],parseInt(s))||s<42&&r(t.day[a],1)||o<30&&r(t.days[a],parseInt(o))||o<45&&r(t.month[a],1)||o<365&&r(t.months[a],parseInt(o/30))||d<1.5&&r(t.year[a],1)||r(t.years[a],parseInt(d))};