@alessiofrittoli/date-utils
Version:
Lightweight TypeScript date utility functions library
2 lines (1 loc) • 3.28 kB
JavaScript
import{a as T,b as I,c as d,d as h,e as F,f as c,g as p}from"./chunk-3N7IV5SJ.mjs";import{a as g,d as D,e as i,f as a,h as w}from"./chunk-YDVKBQB2.mjs";import{englishOrdinalSuffix as y}from"@alessiofrittoli/math-utils/helpers";var s=(o=new Date,u,t,n)=>{let e=new Date(o),r=n;return u?typeof u=="object"?new Intl.DateTimeFormat(t,{...u,timeZone:u.timeZone||r}).format(e):u.split("").map(f=>{switch(f){case"d":return new Intl.DateTimeFormat(t,{day:"2-digit",timeZone:r}).format(e);case"j":return new Intl.DateTimeFormat(t,{day:"numeric",timeZone:r}).format(e);case"D":return new Intl.DateTimeFormat(t,{weekday:"short",timeZone:r}).format(e);case"J":return new Intl.DateTimeFormat(t,{weekday:"narrow"}).format(e);case"l":return new Intl.DateTimeFormat(t,{weekday:"long",timeZone:r}).format(e);case"w":return e.getDay().toString();case"N":return h(e);case"S":return y(e.getDate());case"z":return I(e);case"b":return new Intl.DateTimeFormat(t,{dayPeriod:"long",timeZone:r}).format(e);case"W":return d(e);case"m":return new Intl.DateTimeFormat(t,{month:"2-digit",timeZone:r}).format(e);case"n":return new Intl.DateTimeFormat(t,{month:"numeric",timeZone:r}).format(e);case"M":return new Intl.DateTimeFormat(t,{month:"short",timeZone:r}).format(e);case"F":return new Intl.DateTimeFormat(t,{month:"long",timeZone:r}).format(e);case"E":return new Intl.DateTimeFormat(t,{month:"narrow",timeZone:r}).format(e);case"t":return T(e);case"L":return Number(F(e.getFullYear()));case"Y":return new Intl.DateTimeFormat(t,{year:"numeric",timeZone:r}).format(e);case"y":return new Intl.DateTimeFormat(t,{year:"2-digit",timeZone:r}).format(e);case"a":{let m=Number(s(e,"G",t,n));return c(m).toLowerCase()}case"A":{let m=Number(s(e,"G",t,n));return c(m)}case"B":return p(e,0);case"h":return new Intl.DateTimeFormat(t,{hour:"2-digit",hour12:!0,timeZone:r}).format(e).replace(/(\sAM|\sPM)/gi,"");case"g":return new Intl.DateTimeFormat(t,{hour:"numeric",hour12:!0,timeZone:r}).format(e).replace(/(\sAM|\sPM)/gi,"");case"H":return new Intl.DateTimeFormat(t,{hour:"2-digit",hour12:!1,timeZone:r}).format(e);case"G":return Number(new Intl.DateTimeFormat(t,{hour:"numeric",hour12:!1,timeZone:r}).format(e)).toString();case"i":return new Intl.DateTimeFormat(t,{minute:"numeric",hour12:!1,timeZone:r}).format(e).padStart(2,"0");case"s":return e.getSeconds().toString().padStart(2,"0");case"v":return e.getMilliseconds();case"u":return e.getMilliseconds()*1e3;case"e":return r||g();case"C":return a({date:e,locale:t,timeZone:n,timeZoneName:"long"});case"K":return a({date:e,locale:t,timeZone:n,timeZoneName:"longGeneric"});case"Q":return a({date:e,locale:t,timeZone:n,timeZoneName:"longOffset"});case"q":return s(e,"Q",t,n).replace(/:/g,"");case"R":return a({date:e,locale:t,timeZone:n,timeZoneName:"short"});case"V":return a({date:e,locale:t,timeZone:n,timeZoneName:"shortGeneric"});case"T":return a({date:e,locale:t,timeZone:n,timeZoneName:"shortOffset"});case"I":return Number(w(e,n));case"O":return i(o,n,"");case"P":return i(o,n);case"p":{let m=i(o,n);return m==="+00:00"?"Z":m}case"Z":return D(o,n)*60*60;case"c":return e.toISOString();case"r":return s(e,"D M d Y H:i:s q (C)",t,n);case"U":return e.getTime()/1e3;default:return f}}).join(""):new Intl.DateTimeFormat(t,{timeZone:r}).format(e)};export{s as a};