UNPKG

@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.22 kB
var DateConverter=function(){"use strict";var S=Object.defineProperty;var E=(h,i,c)=>i in h?S(h,i,{enumerable:!0,configurable:!0,writable:!0,value:c}):h[i]=c;var d=(h,i,c)=>E(h,typeof i!="symbol"?i+"":i,c);const i=Symbol.for("constructDateFrom");function c(r,e){return typeof r=="function"?r(e):r&&typeof r=="object"&&i in r?r[i](e):r instanceof Date?new r.constructor(e):new Date(e)}function D(r,e){return c(r,r)}function m(r,e,n){const a=D(r);return isNaN(e)?c(r,NaN):(e&&a.setDate(a.getDate()+e),a)}function l(r){const e=D(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 g(r,...e){const n=c.bind(null,e.find(a=>typeof a=="object"));return e.map(n)}function f(r,e){const n=D(r);return n.setHours(0,0,0,0),n}function p(r,e,n){const[a,o]=g(n==null?void 0:n.in,r,e),s=f(a),u=f(o),y=+s-l(s),Y=+u-l(u);return Math.round((y-Y)/864e5)}function w(r,e){const n=D(r);return n.setHours(23,59,59,999),n}function T(r,e,n){const a=+D(r),[o,s]=[+D(e.start),+D(e.end)].sort((u,y)=>u-y);return a>=o&&a<=s}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 M{constructor(e){d(this,"startDate",new Date("1918-04-13"));d(this,"endDate",new Date("2043-04-13"));d(this,"dateRangeError","The input date is out of supported range.");d(this,"inputYear");d(this,"inputMonth");d(this,"inputDate");const[n,a,o]=this.parseDateString(e);this.inputYear=n,this.inputMonth=a,this.inputDate=o}parseDateString(e){const n=this.numToEn(e).replace(/[./|,]/g,"-").trim(),[a,o,s]=n.split("-").map(Number);return[a,o,s]}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 o=0;for(let s=1975;s<e;s++)o+=t[s][12];for(let s=0;s<n-1;s++)o+=t[e][s];return o+=a-1,m(this.startDate,o)}getBsDate(e){const n=new Date(e);let a=p(n,this.startDate);for(const o in t){const s=t[o][12];if(a>=s){a-=s;continue}for(let u=0;u<12;u++){const y=t[o][u];if(a>=y){a-=y;continue}return{year:Number(o),month:u+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(!T(n,{start:f(this.startDate),end:w(this.endDate)}))throw new Error(this.dateRangeError);const a=this.getBsDate(e),o=this.numToDay(n.getDay());return{...a,day:o}}}return M}(); //# sourceMappingURL=ndc-browser.js.map