-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "nepali-date", | ||
"version": "1.0.0", | ||
"description": "A small Js library to convert English date to Nepali and Vice Versa", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/subeshb1/Nepali-Date.git" | ||
}, | ||
"keywords": [ | ||
"nepali", | ||
"date", | ||
"english", | ||
"date", | ||
"converter" | ||
], | ||
"author": "'Subesh <'subeshb1@gmail.com'>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/subeshb1/Nepali-Date/issues" | ||
}, | ||
"homepage": "https://github.com/subeshb1/Nepali-Date#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
const compose = (...fns) => (...args) => | ||
fns.reduceRight((acc, next) => [next.call(null, ...acc)], args)[0]; | ||
const map = fn => x => x.map(fn); | ||
const add = (a, b) => a + b; | ||
const reduce = fn => acc => x => x.reduce(fn, acc); | ||
const calculateDaysInEachYearD = compose(map(reduce(add)(0))); | ||
|
||
const nepaliYears = [ | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], | ||
[31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], | ||
[31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], | ||
[31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], | ||
[30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], | ||
[30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30] | ||
]; | ||
|
||
const begin = { | ||
year: 2000, | ||
month: 0, | ||
date: 1, | ||
day: 3 | ||
}; | ||
const beginEnglish = { | ||
year: 1943, | ||
month: 3, | ||
date: 14, | ||
day: 3 | ||
}; | ||
const initTime = new Date( | ||
beginEnglish.year, | ||
beginEnglish.month, | ||
beginEnglish.date | ||
); | ||
//2054/06/24 | ||
const dayDiff = (date1, date2) => { | ||
const timeDiff = Math.abs(date2.getTime() - date1.getTime()); | ||
const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); | ||
return diffDays; | ||
}; | ||
|
||
const bsFromDays = difference => { | ||
let bsYear = begin.year; | ||
let bsMonth; | ||
let bsDate = begin.date; | ||
let days = 0; | ||
for (let year of nepaliYears) { | ||
bsMonth = 0; | ||
for (let month of year) { | ||
if (days + month < difference) { | ||
days += month; | ||
} else { | ||
bsDate = difference - days + 1 || month; | ||
return [bsYear, bsMonth, bsDate]; | ||
} | ||
bsMonth++; | ||
} | ||
bsYear++; | ||
} | ||
}; | ||
|
||
const monthsName = [ | ||
"Baisakh", | ||
"Jestha", | ||
"Asadh", | ||
"Shrawan", | ||
"Bhadra", | ||
"Ashwin", | ||
"Kartik", | ||
"Mangshir", | ||
"Poush", | ||
"Magh", | ||
"Falgun", | ||
"Chaitra" | ||
]; | ||
const weekDay = [ | ||
"Sunday", | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday" | ||
]; | ||
|
||
class NepaliDate { | ||
constructor(year = begin.year, month = begin.month, date = begin.date) { | ||
this.year = year || 2000; | ||
this.month = month % 12 || 0; | ||
this.date = date !== 0 ? date : nepaliYears[year % 100][month]; | ||
this.convertBStoAD(); | ||
} | ||
|
||
convertBStoAD() { | ||
const diff = { | ||
year: this.year - begin.year, | ||
month: this.month - begin.month, | ||
date: this.date - begin.date | ||
}; | ||
if (diff.year < 0 || diff.year > 90) | ||
throw new TypeError("Can only support from 2000BS to 2090BS"); | ||
const arr = nepaliYears | ||
.slice(0, diff.year) | ||
.concat([[...nepaliYears[diff.year].slice(0, diff.month), diff.date]]); | ||
let diffDays = reduce(add)(0)(calculateDaysInEachYearD(arr)); | ||
this.ad = new Date( | ||
beginEnglish.year, | ||
beginEnglish.month, | ||
beginEnglish.date + diffDays | ||
); | ||
this.day = this.ad.getDay(); | ||
} | ||
setMonth(month) { | ||
this.month = month; | ||
} | ||
setMonth(year) { | ||
this.year = year; | ||
} | ||
|
||
toAD() { | ||
return { | ||
year: this.ad.getFullYear(), | ||
month: this.ad.getMonth(), | ||
date: this.ad.getDate() | ||
}; | ||
} | ||
toJSDate() { | ||
return new Date(this.ad.getTime()); | ||
} | ||
getDateAD() { | ||
return this.ad.getDate(); | ||
} | ||
getDay() { | ||
return this.day; | ||
} | ||
getMonthAD() { | ||
return this.ad.getMonth(); | ||
} | ||
static fromAD( | ||
year = beginEnglish.year, | ||
month = beginEnglish.month, | ||
date = beginEnglish.date | ||
) { | ||
const difference = dayDiff(initTime, new Date(year, month, date)); | ||
return new NepaliDate(...bsFromDays(difference)); | ||
} | ||
static now() {} | ||
toString() { | ||
return `${weekDay[this.day]}, ${monthsName[this.month]} ${this.date}, ${ | ||
this.year | ||
} BS`; | ||
} | ||
} | ||
|
||
module.exports = NepaliDate; |