Skip to content

Commit 7206211

Browse files
committed
Releasing First Version
1 parent 7a6008c commit 7206211

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "nepali-date",
3+
"version": "1.0.0",
4+
"description": "A small Js library to convert English date to Nepali and Vice Versa",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/subeshb1/Nepali-Date.git"
12+
},
13+
"keywords": [
14+
"nepali",
15+
"date",
16+
"english",
17+
"date",
18+
"converter"
19+
],
20+
"author": "'Subesh <'[email protected]'>",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/subeshb1/Nepali-Date/issues"
24+
},
25+
"homepage": "https://github.com/subeshb1/Nepali-Date#readme"
26+
}

src/index.js

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
const compose = (...fns) => (...args) =>
2+
fns.reduceRight((acc, next) => [next.call(null, ...acc)], args)[0];
3+
const map = fn => x => x.map(fn);
4+
const add = (a, b) => a + b;
5+
const reduce = fn => acc => x => x.reduce(fn, acc);
6+
const calculateDaysInEachYearD = compose(map(reduce(add)(0)));
7+
8+
const nepaliYears = [
9+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
10+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
11+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
12+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
13+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
14+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
15+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
16+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
17+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
18+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
19+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
20+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
21+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
22+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
23+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
24+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
25+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
26+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
27+
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
28+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
29+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
30+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
31+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
32+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
33+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
34+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
35+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
36+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
37+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
38+
[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
39+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
40+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
41+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
42+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
43+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
44+
[30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
45+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
46+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
47+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
48+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
49+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
50+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
51+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
52+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
53+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
54+
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
55+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
56+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
57+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
58+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
59+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
60+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
61+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
62+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
63+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
64+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
65+
[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30],
66+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
67+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
68+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
69+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
70+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
71+
[30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31],
72+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
73+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
74+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
75+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31],
76+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
77+
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30],
78+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
79+
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30],
80+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
81+
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30],
82+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31],
83+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
84+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
85+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
86+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31],
87+
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30],
88+
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30],
89+
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30],
90+
[31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30],
91+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
92+
[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
93+
[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30],
94+
[31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30],
95+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
96+
[31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30],
97+
[30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30],
98+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30],
99+
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30]
100+
];
101+
102+
const begin = {
103+
year: 2000,
104+
month: 0,
105+
date: 1,
106+
day: 3
107+
};
108+
const beginEnglish = {
109+
year: 1943,
110+
month: 3,
111+
date: 14,
112+
day: 3
113+
};
114+
const initTime = new Date(
115+
beginEnglish.year,
116+
beginEnglish.month,
117+
beginEnglish.date
118+
);
119+
//2054/06/24
120+
const dayDiff = (date1, date2) => {
121+
const timeDiff = Math.abs(date2.getTime() - date1.getTime());
122+
const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
123+
return diffDays;
124+
};
125+
126+
const bsFromDays = difference => {
127+
let bsYear = begin.year;
128+
let bsMonth;
129+
let bsDate = begin.date;
130+
let days = 0;
131+
for (let year of nepaliYears) {
132+
bsMonth = 0;
133+
for (let month of year) {
134+
if (days + month < difference) {
135+
days += month;
136+
} else {
137+
bsDate = difference - days + 1 || month;
138+
return [bsYear, bsMonth, bsDate];
139+
}
140+
bsMonth++;
141+
}
142+
bsYear++;
143+
}
144+
};
145+
146+
const monthsName = [
147+
"Baisakh",
148+
"Jestha",
149+
"Asadh",
150+
"Shrawan",
151+
"Bhadra",
152+
"Ashwin",
153+
"Kartik",
154+
"Mangshir",
155+
"Poush",
156+
"Magh",
157+
"Falgun",
158+
"Chaitra"
159+
];
160+
const weekDay = [
161+
"Sunday",
162+
"Monday",
163+
"Tuesday",
164+
"Wednesday",
165+
"Thursday",
166+
"Friday",
167+
"Saturday"
168+
];
169+
170+
class NepaliDate {
171+
constructor(year = begin.year, month = begin.month, date = begin.date) {
172+
this.year = year || 2000;
173+
this.month = month % 12 || 0;
174+
this.date = date !== 0 ? date : nepaliYears[year % 100][month];
175+
this.convertBStoAD();
176+
}
177+
178+
convertBStoAD() {
179+
const diff = {
180+
year: this.year - begin.year,
181+
month: this.month - begin.month,
182+
date: this.date - begin.date
183+
};
184+
if (diff.year < 0 || diff.year > 90)
185+
throw new TypeError("Can only support from 2000BS to 2090BS");
186+
const arr = nepaliYears
187+
.slice(0, diff.year)
188+
.concat([[...nepaliYears[diff.year].slice(0, diff.month), diff.date]]);
189+
let diffDays = reduce(add)(0)(calculateDaysInEachYearD(arr));
190+
this.ad = new Date(
191+
beginEnglish.year,
192+
beginEnglish.month,
193+
beginEnglish.date + diffDays
194+
);
195+
this.day = this.ad.getDay();
196+
}
197+
setMonth(month) {
198+
this.month = month;
199+
}
200+
setMonth(year) {
201+
this.year = year;
202+
}
203+
204+
toAD() {
205+
return {
206+
year: this.ad.getFullYear(),
207+
month: this.ad.getMonth(),
208+
date: this.ad.getDate()
209+
};
210+
}
211+
toJSDate() {
212+
return new Date(this.ad.getTime());
213+
}
214+
getDateAD() {
215+
return this.ad.getDate();
216+
}
217+
getDay() {
218+
return this.day;
219+
}
220+
getMonthAD() {
221+
return this.ad.getMonth();
222+
}
223+
static fromAD(
224+
year = beginEnglish.year,
225+
month = beginEnglish.month,
226+
date = beginEnglish.date
227+
) {
228+
const difference = dayDiff(initTime, new Date(year, month, date));
229+
return new NepaliDate(...bsFromDays(difference));
230+
}
231+
static now() {}
232+
toString() {
233+
return `${weekDay[this.day]}, ${monthsName[this.month]} ${this.date}, ${
234+
this.year
235+
} BS`;
236+
}
237+
}
238+
239+
module.exports = NepaliDate;

0 commit comments

Comments
 (0)