patro
Version:
AD and BS Calendar
2 lines (1 loc) • 15.8 kB
JavaScript
;var t=require("react/jsx-runtime"),e=require("react");const a="1921-04-13",r=1978,n=+(new Date).getFullYear(),o=+(new Date).getMonth()+1,s=+(new Date).getDate(),h={en:{months:{January:"Jan",February:"Feb",March:"Mar",April:"Apr",May:"May",June:"Jun",July:"Jul",August:"Aug",September:"Sep",October:"Oct",November:"Nov",December:"Dec"},days:{Sunday:"Sun",Monday:"Mon",Tuesday:"Tue",Wednesday:"Wed",Thursday:"Thu",Friday:"Fri",Saturday:"Sat"}},np:{months:{"बैशाख":"बै","जेष्ठ":"जे","आषाढ":"आषा","श्रावण":"श्रा","भाद्र":"भा","आश्विन":"आश","कार्तिक":"का","मंसिर":"मं","पौष":"पौ","माघ":"मा","फाल्गुन":"फा","चैत्र":"चै"},days:{"आइतवार":"आइ","सोमवार":"सोम","मंगलवार":"मंगल","बुधवार":"बुध","बिहिवार":"बिहि","शुक्रवार":"शुक्र","शनिवार":"शनि"},numbers:{0:"०",1:"१",2:"२",3:"३",4:"४",5:"५",6:"६",7:"७",8:"८",9:"९"}}},d=Object.values(h.en.days).length,_=[];_[1978]=[1978,31,31,32,31,31,31,30,29,30,29,30,30],_[1979]=[1979,31,31,32,32,31,30,30,29,30,29,30,30],_[1980]=[1980,31,32,31,32,31,30,30,30,29,29,30,31],_[1981]=[1981,31,31,31,32,31,31,29,30,30,29,30,30],_[1982]=[1982,31,31,32,31,31,31,30,29,30,29,30,30],_[1983]=[1983,31,31,32,32,31,30,30,29,30,29,30,30],_[1984]=[1984,31,32,31,32,31,30,30,30,29,29,30,31],_[1985]=[1985,31,31,31,32,31,31,29,30,30,29,30,30],_[1986]=[1986,31,31,32,31,31,31,30,29,30,29,30,30],_[1987]=[1987,31,32,31,32,31,30,30,29,30,29,30,30],_[1988]=[1988,31,32,31,32,31,30,30,30,29,29,30,31],_[1989]=[1989,31,31,31,32,31,31,30,29,30,29,30,30],_[1990]=[1990,31,31,32,31,31,31,30,29,30,29,30,30],_[1991]=[1991,31,32,31,32,31,30,30,29,30,29,30,30],_[1992]=[1992,31,32,31,32,31,30,30,30,29,30,29,31],_[1993]=[1993,31,31,31,32,31,31,30,29,30,29,30,30],_[1994]=[1994,31,31,32,31,31,31,30,29,30,29,30,30],_[1995]=[1995,31,32,31,32,31,30,30,30,29,29,30,30],_[1996]=[1996,31,32,31,32,31,30,30,30,29,30,29,31],_[1997]=[1997,31,31,32,31,31,31,30,29,30,29,30,30],_[1998]=[1998,31,31,32,31,31,31,30,29,30,29,30,30],_[1999]=[1999,31,32,31,32,31,30,30,30,29,29,30,31],_[2e3]=[2e3,30,32,31,32,31,30,30,30,29,30,29,31],_[2001]=[2001,31,31,32,31,31,31,30,29,30,29,30,30],_[2002]=[2002,31,31,32,32,31,30,30,29,30,29,30,30],_[2003]=[2003,31,32,31,32,31,30,30,30,29,29,30,31],_[2004]=[2004,30,32,31,32,31,30,30,30,29,30,29,31],_[2005]=[2005,31,31,32,31,31,31,30,29,30,29,30,30],_[2006]=[2006,31,31,32,32,31,30,30,29,30,29,30,30],_[2007]=[2007,31,32,31,32,31,30,30,30,29,29,30,31],_[2008]=[2008,31,31,31,32,31,31,29,30,30,29,29,31],_[2009]=[2009,31,31,32,31,31,31,30,29,30,29,30,30],_[2010]=[2010,31,31,32,32,31,30,30,29,30,29,30,30],_[2011]=[2011,31,32,31,32,31,30,30,30,29,29,30,31],_[2012]=[2012,31,31,31,32,31,31,29,30,30,29,30,30],_[2013]=[2013,31,31,32,31,31,31,30,29,30,29,30,30],_[2014]=[2014,31,31,32,32,31,30,30,29,30,29,30,30],_[2015]=[2015,31,32,31,32,31,30,30,30,29,29,30,31],_[2016]=[2016,31,31,31,32,31,31,29,30,30,29,30,30],_[2017]=[2017,31,31,32,31,31,31,30,29,30,29,30,30],_[2018]=[2018,31,32,31,32,31,30,30,29,30,29,30,30],_[2019]=[2019,31,32,31,32,31,30,30,30,29,30,29,31],_[2020]=[2020,31,31,31,32,31,31,30,29,30,29,30,30],_[2021]=[2021,31,31,32,31,31,31,30,29,30,29,30,30],_[2022]=[2022,31,32,31,32,31,30,30,30,29,29,30,30],_[2023]=[2023,31,32,31,32,31,30,30,30,29,30,29,31],_[2024]=[2024,31,31,31,32,31,31,30,29,30,29,30,30],_[2025]=[2025,31,31,32,31,31,31,30,29,30,29,30,30],_[2026]=[2026,31,32,31,32,31,30,30,30,29,29,30,31],_[2027]=[2027,30,32,31,32,31,30,30,30,29,30,29,31],_[2028]=[2028,31,31,32,31,31,31,30,29,30,29,30,30],_[2029]=[2029,31,31,32,31,32,30,30,29,30,29,30,30],_[2030]=[2030,31,32,31,32,31,30,30,30,29,29,30,31],_[2031]=[2031,30,32,31,32,31,30,30,30,29,30,29,31],_[2032]=[2032,31,31,32,31,31,31,30,29,30,29,30,30],_[2033]=[2033,31,31,32,32,31,30,30,29,30,29,30,30],_[2034]=[2034,31,32,31,32,31,30,30,30,29,29,30,31],_[2035]=[2035,30,32,31,32,31,31,29,30,30,29,29,31],_[2036]=[2036,31,31,32,31,31,31,30,29,30,29,30,30],_[2037]=[2037,31,31,32,32,31,30,30,29,30,29,30,30],_[2038]=[2038,31,32,31,32,31,30,30,30,29,29,30,31],_[2039]=[2039,31,31,31,32,31,31,29,30,30,29,30,30],_[2040]=[2040,31,31,32,31,31,31,30,29,30,29,30,30],_[2041]=[2041,31,31,32,32,31,30,30,29,30,29,30,30],_[2042]=[2042,31,32,31,32,31,30,30,30,29,29,30,31],_[2043]=[2043,31,31,31,32,31,31,29,30,30,29,30,30],_[2044]=[2044,31,31,32,31,31,31,30,29,30,29,30,30],_[2045]=[2045,31,32,31,32,31,30,30,29,30,29,30,30],_[2046]=[2046,31,32,31,32,31,30,30,30,29,29,30,31],_[2047]=[2047,31,31,31,32,31,31,30,29,30,29,30,30],_[2048]=[2048,31,31,32,31,31,31,30,29,30,29,30,30],_[2049]=[2049,31,32,31,32,31,30,30,30,29,29,30,30],_[2050]=[2050,31,32,31,32,31,30,30,30,29,30,29,31],_[2051]=[2051,31,31,31,32,31,31,30,29,30,29,30,30],_[2052]=[2052,31,31,32,31,31,31,30,29,30,29,30,30],_[2053]=[2053,31,32,31,32,31,30,30,30,29,29,30,30],_[2054]=[2054,31,32,31,32,31,30,30,30,29,30,29,31],_[2055]=[2055,31,31,32,31,31,31,30,29,30,29,30,30],_[2056]=[2056,31,31,32,31,32,30,30,29,30,29,30,30],_[2057]=[2057,31,32,31,32,31,30,30,30,29,29,30,31],_[2058]=[2058,30,32,31,32,31,30,30,30,29,30,29,31],_[2059]=[2059,31,31,32,31,31,31,30,29,30,29,30,30],_[2060]=[2060,31,31,32,32,31,30,30,29,30,29,30,30],_[2061]=[2061,31,32,31,32,31,30,30,30,29,29,30,31],_[2062]=[2062,31,31,31,32,31,31,29,30,29,30,29,31],_[2063]=[2063,31,31,32,31,31,31,30,29,30,29,30,30],_[2064]=[2064,31,31,32,32,31,30,30,29,30,29,30,30],_[2065]=[2065,31,32,31,32,31,30,30,30,29,29,30,31],_[2066]=[2066,31,31,31,32,31,31,29,30,30,29,29,31],_[2067]=[2067,31,31,32,31,31,31,30,29,30,29,30,30],_[2068]=[2068,31,31,32,32,31,30,30,29,30,29,30,30],_[2069]=[2069,31,32,31,32,31,30,30,30,29,29,30,31],_[2070]=[2070,31,31,31,32,31,31,29,30,30,29,30,30],_[2071]=[2071,31,31,32,31,31,31,30,29,30,29,30,30],_[2072]=[2072,31,32,31,32,31,30,30,29,30,29,30,30],_[2073]=[2073,31,32,31,32,31,30,30,30,29,29,30,31],_[2074]=[2074,31,31,31,32,31,31,30,29,30,29,30,30],_[2075]=[2075,31,31,32,31,31,31,30,29,30,29,30,30],_[2076]=[2076,31,32,31,32,31,30,30,30,29,29,30,30],_[2077]=[2077,31,32,31,32,31,30,30,30,29,30,29,31],_[2078]=[2078,31,31,31,32,31,31,30,29,30,29,30,30],_[2079]=[2079,31,31,32,31,31,31,30,29,30,29,30,30],_[2080]=[2080,31,32,31,32,31,30,30,30,29,29,30,30],_[2081]=[2081,31,31,32,32,31,30,30,30,29,30,30,30],_[2082]=[2082,30,32,31,32,31,30,30,30,29,30,30,30],_[2083]=[2083,31,31,32,31,31,30,30,30,29,30,30,30],_[2084]=[2084,31,31,32,31,31,30,30,30,29,30,30,30],_[2085]=[2085,31,32,31,32,30,31,30,30,29,30,30,30],_[2086]=[2086,30,32,31,32,31,30,30,30,29,30,30,30],_[2087]=[2087,31,31,32,31,31,31,30,30,29,30,30,30],_[2088]=[2088,30,31,32,32,30,31,30,30,29,30,30,30],_[2089]=[2089,30,32,31,32,31,30,30,30,29,30,30,30],_[2090]=[2090,30,32,31,32,31,30,30,30,29,30,30,30],_[2091]=[2091,31,31,32,31,31,31,30,30,29,30,30,30],_[2092]=[2092,30,31,32,32,31,30,30,30,29,30,30,30],_[2093]=[2093,30,32,31,32,31,30,30,30,29,30,30,30],_[2094]=[2094,31,31,32,31,31,30,30,30,29,30,30,30],_[2095]=[2095,31,31,32,31,31,31,30,29,30,30,30,30],_[2096]=[2096,30,31,32,32,31,30,30,29,30,29,30,30],_[2097]=[2097,31,32,31,32,31,30,30,30,29,30,30,30],_[2098]=[2098,31,31,32,31,31,31,29,30,29,30,29,31],_[2099]=[2099,31,32,31,32,31,30,30,30,29,29,30,31];const l=t=>{const{year:e,month:a,day:n}=N(t);if((t=>t<r||t>2099)(e))throw new Error("BS Date out of range");if(((t,e,a)=>t>_[e][a])(n,e,a))throw new Error(`No ${n} day exits in this month.`);return S(e,a,n)},i=t=>{const[e,n,o]=l(t).split("-").map((t=>parseInt(t)));let s=0;for(let t=r;t<=e;t++)if(t===e){for(let e=1;e<n;e++)s+=_[t][e];s+=o-1}else for(let e=1;e<=12;e++)s+=_[t][e];return((t,e)=>{const a=new Date(t);a.setDate(a.getDate()+e);const r=a.getFullYear(),n=a.getMonth()+1,o=a.getDate();return`${r}-${n>9?n:"0"+n}-${o>9?o:"0"+o}`})(a,s)},{en:p,np:c}=h,u=t=>{for(let e=0;e<t.length;e++){const a=t[e].findIndex((t=>"०१"===t.extraDate));if(a>-1)return a}},m=(t,e)=>{if(t>12){const t=e+1;return{bsYearDates:_[t],bsMonthCount:_[t][1]}}if(t<1){const t=e-1;return{bsYearDates:_[t],bsMonthCount:_[t][12]}}return{bsYearDates:_[e],bsMonthCount:_[e][t]}},y=(...t)=>{const[e,a,r,n,o,s]=t,{year:h,month:_}=j(r,n);let l=[];const p=[],[c,y,f]=e.split("-"),D=u(w(h,_,o,s)),g=u(w(h,_+1,o,s)),{bsMonthCount:x}=m(parseInt(E(y))-1,parseInt(c)),{bsMonthCount:M}=m(parseInt(E(y)),parseInt(c)),{bsMonthCount:C}=m(parseInt(E(y))+1,parseInt(c)),b=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=j(r,n);return 0!=a?Array.from({length:e},((t,e)=>({dayOfMonth:O(String(e+1)),year:r,month:s,adDate:i(S(o,s,e+1)),bsDate:S(o,s,e+1),extraDate:i(S(o,s,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_prev_month_date_offset"}))).slice(-a):[]})(x,D,c,+E(y)-1),v=((...t)=>{const[e,a,r]=t;return Array.from({length:e},((t,e)=>({dayOfMonth:O(String(e+1)),year:a,month:r,adDate:i(S(a,r,e+1)),bsDate:S(a,r,e+1),extraDate:i(S(a,r,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date"})))})(M,c,+E(y)),A=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=j(r,n);return Array.from({length:e},((t,e)=>({dayOfMonth:O(String(e+1)),year:o,month:s+1,adDate:i(S(o,s,e+1)),bsDate:S(o,s,e+1),extraDate:i(S(o,s,e+1)).split("-")[2],type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_next_month_date_offset"}))).slice(0,d-a)})(C,g,c,+E(y)+1),N=[...b,...v,...A];return N.forEach(((t,e)=>{e%d!=0||(p.push(l),l=[]),l.push(t),e===N.length-1&&p.push(l)})),p},{np:f,en:D}=h,g=t=>{const e=new Date(a),n=new Date((t=>{const{year:e,month:a,day:r}=N(t);if((t=>t>2040)(e))throw new Error("AD Date out of range");if((t=>t>31)(a))throw new Error("Month is invalid.");return S(e,a,r)})(t)),o=Math.floor((n.getTime()-e.getTime())/864e5);if(o<0)throw new Error("AD Date out of range");return(t=>{let e=0,a=0,n=0,o=0,s=!1;for(let h=r;h<2100&&!s;h++)for(let r=1;r<=12;r++)if(o+=_[h][r],t-o<0){n=t-o+_[h][r]+1,s=!0,e=h,a=r;break}return S(e,a,n)})(o)},w=(...t)=>{let e=[];const a=[],[r,n,o,s]=t,{year:h,month:_}=j(r,n),l=C(_-1,h),i=C(_,h),p=C(_+1,h),c=b(_,h),u=b(_+1,h),m=((...t)=>{const[e,a,r,n]=t,o=r-1<=0?"01":r-1;return 0!=a?Array.from({length:e},((t,e)=>({dayOfMonth:e+1,year:n,month:r-1,adDate:S(n,r-1,e+1),extraDate:O(g(S(n,+o,e+1)).split("-")[2]),type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_prev_month_date_offset"}))).slice(-a):[]})(l,c,_,h),y=((...t)=>{const[e,a,r,n]=t,{year:o,month:s}=j(n,r+1);return Array.from({length:e},((t,e)=>({dayOfMonth:e+1,year:o,month:s+1,adDate:S(o,s,e+1),extraDate:O(g(S(o,s,e+1)).split("-")[2]),type:"_patro_dates_date _patro_dates_current_month_date _patro_dates_date_offset _patro_dates_next_month_date_offset"}))).slice(0,d-a)})(p,u,_,h),f=[...m,...v(i,_,h,o,s),...y];return f.forEach(((t,r)=>{r%d!=0||(a.push(e),e=[]),e.push(t),r===f.length-1&&a.push(e)})),a},{en:x,np:M}=h,C=(t=o,e=n)=>new Date(e,t,0).getDate(),b=(t=o,e=n)=>+new Date(`${e}-${A(t)}-01`).getDay(),j=(t,e)=>e>12?{year:+t+1,month:1}:e<=0?{year:+t-1,month:12}:{year:t,month:e},v=(...t)=>{const[e,a,r,n,o]=t,{month:s}=o;return Array.from({length:e},((t,e)=>{const o=e+1,h=S(r,a,e+1),d=O(g(S(r,a,e+1)).split("-")[2]);return{dayOfMonth:o,year:r,month:a,adDate:h,extraDate:d,type:"_patro_dates_date _patro_dates_current_month_date "+(a===s&&n===e+1?"_patro_dates_current_date":"")}}))},A=t=>("0"+t).slice(-2),S=(t,e,a)=>`${t}-${A(e)}-${A(a)||"01"}`,N=t=>{if(3!==t.split("-").length)throw new Error("Date format is invalid");const[e,a,r]=t.split("-").map((t=>parseInt(t)));if((t=>t>32||t<1||!t)(r))throw new Error("Day is invalid.");if((t=>t>12||t<1||!t)(a))throw new Error("Month is invalid.");if((t=>!t||t.toString().length<4)(e))throw new Error("Year is invalid.");return{year:e,month:a,day:r}},O=t=>t.split("").map((t=>M.numbers[t])).join(""),E=t=>t.replace(/^0+/,""),$=(t,...e)=>"AD"===t?((t,e,a,r)=>{const n=parseInt(E(t)),o=Object.keys(f.months),s=n-2;return{oMonth:`${o[s<0?11:s]}/${o[n-1]} - `,month:Object.keys(D.months)[e-1],oYear:a,year:r}})(...e):((t,e,a,r)=>{const n=parseInt(E(t)),o=Object.values(p.months),s=e-1;return{oMonth:`${o[s<0?11:s]}/${o[12===e?0:e]} - `,month:Object.keys(c.months)[n-1],oYear:r,year:a}})(...e),{en:I,np:Y}=h,k=()=>t.jsx("svg",{width:"26",height:"26",viewBox:"0 0 20 20",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:t.jsx("path",{d:"M12.9499 9.40832L8.23328 4.69999C8.15581 4.62188 8.06364 4.55989 7.96209 4.51758C7.86054 4.47527 7.75162 4.45349 7.64161 4.45349C7.5316 4.45349 7.42268 4.47527 7.32113 4.51758C7.21958 4.55989 7.12741 4.62188 7.04994 4.69999C6.89474 4.85613 6.80762 5.06734 6.80762 5.28749C6.80762 5.50764 6.89474 5.71885 7.04994 5.87499L11.1749 10.0417L7.04994 14.1667C6.89474 14.3228 6.80762 14.534 6.80762 14.7542C6.80762 14.9743 6.89474 15.1855 7.04994 15.3417C7.12712 15.4204 7.21916 15.483 7.32072 15.526C7.42229 15.5689 7.53135 15.5912 7.64161 15.5917C7.75187 15.5912 7.86094 15.5689 7.9625 15.526C8.06406 15.483 8.1561 15.4204 8.23328 15.3417L12.9499 10.6333C13.0345 10.5553 13.102 10.4606 13.1482 10.3552C13.1944 10.2497 13.2182 10.1359 13.2182 10.0208C13.2182 9.90574 13.1944 9.7919 13.1482 9.68648C13.102 9.58107 13.0345 9.48636 12.9499 9.40832Z",fill:"#121212"})}),F=()=>t.jsx("svg",{width:"26",height:"26",viewBox:"0 0 20 20",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:t.jsx("path",{d:"M7.08325 10.6666L11.8333 15.3333C12.1666 15.6666 12.6666 15.6666 12.9999 15.3333C13.3333 15 13.3333 14.5 12.9999 14.1666L8.91659 9.99996L12.9999 5.83329C13.3333 5.49996 13.3333 4.99996 12.9999 4.66663C12.8333 4.49996 12.6666 4.41663 12.4166 4.41663C12.1666 4.41663 11.9999 4.49996 11.8333 4.66663L7.08325 9.33329C6.74992 9.74996 6.74992 10.25 7.08325 10.6666C7.08325 10.5833 7.08325 10.5833 7.08325 10.6666Z",fill:"#121212"})});exports.Calendar=a=>{const{onChange:r,calendarType:h="AD",showExtra:d}=a,[_,l]=e.useState(new Date),[i,p,c,u]=((t,a)=>{const[r,h]=e.useState(n),[d,_]=e.useState(o),[l,i]=e.useState(s),[p,c]=e.useState({month:o,year:n,date:s});e.useEffect((()=>{a&&"getMonth"in a&&(_((()=>a.getMonth()+1)),h((()=>a.getFullYear())),i((()=>a.getDate())),c({month:a.getMonth(),year:a.getFullYear(),date:a.getDate()}))}),[]);const u=e.useCallback((()=>{12===d?(h((t=>t+1)),_((()=>1))):_((t=>t+1))}),[d]),m=e.useCallback((()=>{d<=1?(h((t=>t-1)),_((()=>12))):_((t=>t-1))}),[d]),f=e.useMemo((()=>w(r,d,l,p)),[r,d,l]),D=e.useMemo((()=>g(S(r,d,l))),[r,d,l]),[x,M]=D.split("-"),C={adDate:S(r,d,l),bsDate:D,headerMonths:{...$(t,M,d,x,String(r))}},b=e.useMemo((()=>y(D,f,r,d,l,p)),[D,f,r,d,l,p]);return["BS"===t?b:f,C,u,m]})(h,_),{headerMonths:{month:m,oMonth:f,oYear:D,year:x}}=p||{};return t.jsx("div",{className:"App",children:t.jsxs("div",{className:"_patro_container _patro",children:[t.jsxs("header",{className:"_patro_header",children:[t.jsx("button",{className:"_patro_header_actionBtn",onClick:()=>u(),children:t.jsx(F,{})}),t.jsxs("h3",{className:"_patro_header_info",children:[t.jsxs("p",{className:"_patro_header_info_month_year",children:[m," ",x]}),t.jsxs("p",{className:"_patro_header_info_month_year",children:[f," ",D]})]}),t.jsx("button",{className:"_patro_header_actionBtn",onClick:()=>c(),children:t.jsx(k,{})})]}),t.jsx("div",{className:"_patro_days",children:Object.values("AD"===h?I.days:Y.days).map(((e,a)=>t.jsx("div",{className:"_patro_days_day",children:e},a+e)))}),i.map(((e,a)=>t.jsx("div",{className:"_patro_dates",children:Array.isArray(e)&&e.map(((e,n)=>t.jsxs("p",{title:e.adDate,onClick:()=>(t=>{l((()=>new Date(t.adDate))),r&&r(t)})(e),className:e.type,children:[e.dayOfMonth,d&&t.jsx("sub",{children:e.extraDate})]},a+n)))},a)))]})})};