UNPKG

itty-time

Version:

Ultra-small (~390 bytes) library for TTL date math and converting ms durations to and from strings.

3 lines (2 loc) 542 B
"use strict";const e=36e5,t=24*e,s={year:315576e5,month:30*t,week:7*t,day:t,hour:e,minute:6e4,second:1e3,m:1},r=e=>{if(+e)return+e;const[,t,r]=e.match(/^([^ ]+) +(\w\w*?)s?$/)||[];return+t*(s[r]||1)};exports.datePlus=(e,t=new Date)=>new Date(t.getTime()+r(e)),exports.duration=(e,{parts:t=9,join:r=", "}={})=>{let n,o=[];for(let[u,a]of Object.entries(s))e>=a&&t-- >0&&(e-=(n=e/a|0)*a,t||(n+=e/a),1==n&&"m"!=u||(u+="s"),o.push(r?n+" "+u:[u,n]));return r?o.join(r):o},exports.ms=r,exports.seconds=e=>r(e)/1e3; //# sourceMappingURL=index.js.map