@remotemerge/nepali-date-converter
Version:
A modern JavaScript library for seamless conversion between Bikram Sambat (BS) and Gregorian (AD) dates. Easily convert Nepali dates to English dates and vice versa with support for years 1975 BS to 2099 BS.
3 lines (2 loc) • 5.2 kB
JavaScript
;var m=Object.defineProperty;var p=(r,e,n)=>e in r?m(r,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[e]=n;var u=(r,e,n)=>p(r,typeof e!="symbol"?e+"":e,n);const y=Symbol.for("constructDateFrom");function h(r,e){return typeof r=="function"?r(e):r&&typeof r=="object"&&y in r?r[y](e):r instanceof Date?new r.constructor(e):new Date(e)}function c(r,e){return h(r,r)}function g(r,e,n){const a=c(r);return isNaN(e)?h(r,NaN):(e&&a.setDate(a.getDate()+e),a)}function f(r){const e=c(r),n=new Date(Date.UTC(e.getFullYear(),e.getMonth(),e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),e.getMilliseconds()));return n.setUTCFullYear(e.getFullYear()),+r-+n}function w(r,...e){const n=h.bind(null,e.find(a=>typeof a=="object"));return e.map(n)}function d(r,e){const n=c(r);return n.setHours(0,0,0,0),n}function T(r,e,n){const[a,s]=w(n==null?void 0:n.in,r,e),o=d(a),i=d(s),D=+o-f(o),l=+i-f(i);return Math.round((D-l)/864e5)}function M(r,e){const n=c(r);return n.setHours(23,59,59,999),n}function Y(r,e,n){const a=+c(r),[s,o]=[+c(e.start),+c(e.end)].sort((i,D)=>i-D);return a>=s&&a<=o}const t=[];t[1975]=[31,31,32,32,31,30,30,29,30,29,30,30,365];t[1976]=[31,32,31,32,31,30,30,30,29,29,30,31,366];t[1977]=[30,32,31,32,31,31,29,30,30,29,29,31,365];t[1978]=[31,31,32,31,31,31,30,29,30,29,30,30,365];t[1979]=t[1975];t[1980]=t[1976];t[1981]=[31,31,31,32,31,31,29,30,30,29,29,31,365];t[1982]=t[1978];t[1983]=t[1975];t[1984]=t[1976];t[1985]=[31,31,31,32,31,31,29,30,30,29,30,30,365];t[1986]=t[1978];t[1987]=[31,32,31,32,31,30,30,29,30,29,30,30,365];t[1988]=t[1976];t[1989]=[31,31,31,32,31,31,30,29,30,29,30,30,365];t[1990]=t[1978];t[1991]=[31,32,31,32,31,30,30,30,29,29,30,30,365];t[1992]=[31,32,31,32,31,30,30,30,29,30,29,31,366];t[1993]=t[1989];t[1994]=t[1978];t[1995]=t[1991];t[1996]=t[1992];t[1997]=t[1978];t[1998]=[31,31,32,31,32,30,30,29,30,29,30,30,365];t[1999]=t[1976];t[2e3]=[30,32,31,32,31,30,30,30,29,30,29,31,365];t[2001]=t[1978];t[2002]=t[1975];t[2003]=t[1976];t[2004]=t[2e3];t[2005]=t[1978];t[2006]=t[1975];t[2007]=t[1976];t[2008]=t[1981];t[2009]=t[1978];t[2010]=t[1975];t[2011]=t[1976];t[2012]=t[1985];t[2013]=t[1978];t[2014]=t[1975];t[2015]=t[1976];t[2016]=t[1985];t[2017]=t[1978];t[2018]=t[1987];t[2019]=t[1992];t[2020]=t[1989];t[2021]=t[1978];t[2022]=t[1991];t[2023]=t[1992];t[2024]=t[1989];t[2025]=t[1978];t[2026]=t[1976];t[2027]=t[2e3];t[2028]=t[1978];t[2029]=t[1998];t[2030]=t[1976];t[2031]=t[2e3];t[2032]=t[1978];t[2033]=t[1975];t[2034]=t[1976];t[2035]=t[1977];t[2036]=t[1978];t[2037]=t[1975];t[2038]=t[1976];t[2039]=t[1985];t[2040]=t[1978];t[2041]=t[1975];t[2042]=t[1976];t[2043]=t[1985];t[2044]=t[1978];t[2045]=t[1987];t[2046]=t[1976];t[2047]=t[1989];t[2048]=t[1978];t[2049]=t[1991];t[2050]=t[1992];t[2051]=t[1989];t[2052]=t[1978];t[2053]=t[1991];t[2054]=t[1992];t[2055]=t[1978];t[2056]=t[1998];t[2057]=t[1976];t[2058]=t[2e3];t[2059]=t[1978];t[2060]=t[1975];t[2061]=t[1976];t[2062]=[30,32,31,32,31,31,29,30,29,30,29,31,365];t[2063]=t[1978];t[2064]=t[1975];t[2065]=t[1976];t[2066]=t[1981];t[2067]=t[1978];t[2068]=t[1975];t[2069]=t[1976];t[2070]=t[1985];t[2071]=t[1978];t[2072]=t[1987];t[2073]=t[1976];t[2074]=t[1989];t[2075]=t[1978];t[2076]=t[1991];t[2077]=t[1992];t[2078]=t[1989];t[2079]=t[1978];t[2080]=t[1991];t[2081]=t[1992];t[2082]=t[1989];t[2083]=t[1978];t[2084]=t[1976];t[2085]=t[2e3];t[2086]=t[1978];t[2087]=t[1975];t[2088]=t[1976];t[2089]=t[2e3];t[2090]=t[1978];t[2091]=t[1975];t[2092]=t[1976];t[2093]=t[1981];t[2094]=t[1978];t[2095]=t[1975];t[2096]=t[1976];t[2097]=t[1985];t[2098]=t[1978];t[2099]=t[1975];class S{constructor(e){u(this,"startDate",new Date("1918-04-13"));u(this,"endDate",new Date("2043-04-13"));u(this,"dateRangeError","The input date is out of supported range.");u(this,"inputYear");u(this,"inputMonth");u(this,"inputDate");const[n,a,s]=this.parseDateString(e);this.inputYear=n,this.inputMonth=a,this.inputDate=s}parseDateString(e){const n=this.numToEn(e).replace(/[./|,]/g,"-").trim(),[a,s,o]=n.split("-").map(Number);return[a,s,o]}numToEn(e){const n=["०","१","२","३","४","५","६","७","८","९"];return e.replace(/[०१२३४५६७८९]/g,a=>n.indexOf(a).toString())}numToDay(e){return["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][e]}getAdDate(e,n,a){let s=0;for(let o=1975;o<e;o++)s+=t[o][12];for(let o=0;o<n-1;o++)s+=t[e][o];return s+=a-1,g(this.startDate,s)}getBsDate(e){const n=new Date(e);let a=T(n,this.startDate);for(const s in t){const o=t[s][12];if(a>=o){a-=o;continue}for(let i=0;i<12;i++){const D=t[s][i];if(a>=D){a-=D;continue}return{year:Number(s),month:i+1,date:a+1}}}throw new Error(this.dateRangeError)}toAd(){if(this.inputYear<1975||this.inputYear>2099)throw new Error(this.dateRangeError);const e=this.getAdDate(this.inputYear,this.inputMonth,this.inputDate),n=this.numToDay(e.getDay());return{year:e.getFullYear(),month:e.getMonth()+1,date:e.getDate(),day:n}}toBs(){const e=`${this.inputYear}-${this.inputMonth}-${this.inputDate}`,n=new Date(e);if(!Y(n,{start:d(this.startDate),end:M(this.endDate)}))throw new Error(this.dateRangeError);const a=this.getBsDate(e),s=this.numToDay(n.getDay());return{...a,day:s}}}module.exports=S;
//# sourceMappingURL=ndc-node.js.map