UNPKG

@alessiofrittoli/date-utils

Version:

Lightweight TypeScript date utility functions library

2 lines (1 loc) 1.1 kB
import{padStart as a}from"@alessiofrittoli/math-utils";var D=()=>Intl.DateTimeFormat().resolvedOptions().timeZone,f=(e=new Date)=>-(new Date(e).getTimezoneOffset()/60),o=e=>{if(e.endsWith("Z"))return 0;let t=e.replace(/:/g,"").match(/GMT([+-]\d{4})/);if(!t?.[1])return f(e);let n=t[1],r=parseInt(n.slice(0,3),10),m=parseInt(n.slice(3),10);return(r*60*60+m*60)/60/60},s=(e,t)=>t?t==="UTC"?0:typeof e=="string"?o(e)||o(i({date:e,timeZone:t,timeZoneName:"longOffset"})):o(i({date:e,timeZone:t,timeZoneName:"longOffset"})):typeof e=="string"?o(e):f(e),p=(e,t,n=":")=>{let r=-(s(e,t)*60);return(r<=0?"+":"-")+a(parseInt(String(Math.abs(r/60))),2)+(n||"")+a(Math.abs(r%60),2)},i=e=>Intl.DateTimeFormat(e?.locale,{timeZoneName:e?.timeZoneName||"shortOffset",timeZone:e?.timeZone}).formatToParts(e?.date?new Date(e.date):void 0).find(t=>t.type==="timeZoneName").value,c=(e=new Date,t)=>{let n=new Date(e),r=new Date(n.getFullYear(),0,1),m=new Date(n.getFullYear(),6,1);return Math.max(s(r,t)*-60,s(m,t)*-60)},z=(e=new Date,t)=>s(e,t)*-60<c(e,t);export{D as a,f as b,o as c,s as d,p as e,i as f,c as g,z as h};