bs-calendar-react
Version:
A npm package for nepali calendar with holiday.
903 lines (899 loc) • 22.9 kB
JavaScript
var MS_PER_DAY = 86400000;
var MONTH_NAMES = ["बैशाख", "जेठ", "असार", "साउन", "भदौ", "असोज", "कार्तिक", "मंसिर", "पौष", "माघ", "फाल्गुन", "चैत"];
// ------ TO UPDATE THESE HARDCODED VALUES USE /scripts/encode-days-in-month.js
// We have defined our own Epoch for Bikram Sambat: 1970-1-1 BS or 1913-4-13 AD
var BS_EPOCH_TS = -17899902e5; // = Date.parse('1913-4-13')
var BS_YEAR_ZERO = 1970;
var ENCODED_MONTH_LENGTHS = [
5315258, 5314490, 9459438, 8673005, 5315258, 5315066, 9459438, 8673005, 5315258, 5314298, 9459438, 5327594, 5315258, 5314298,
9459438, 5327594, 5315258, 5314286, 9459438, 5315306, 5315258, 5314286, 8673006, 5315306, 5315258, 5265134, 8673006, 5315258,
5315258, 9459438, 8673005, 5315258, 5314298, 9459438, 8673005, 5315258, 5314298, 9459438, 8473322, 5315258, 5314298, 9459438,
5327594, 5315258, 5314298, 9459438, 5327594, 5315258, 5314286, 8673006, 5315306, 5315258, 5265134, 8673006, 5315306, 5315258,
9459438, 8673005, 5315258, 5314490, 9459438, 8673005, 5315258, 5314298, 9459438, 8473325, 5315258, 5314298, 9459438, 5327594,
5315258, 5314298, 9459438, 5327594, 5315258, 5314286, 9459438, 5315306, 5315258, 5265134, 8673006, 5315306, 5315258, 5265134,
8673006, 5315258, 5314490, 9459438, 8673005, 5315258, 5314298, 9459438, 8669933, 5315258, 5314298, 9459438, 8473322, 5315258,
5314298, 9459438, 5327594, 5315258, 5314286, 9459438, 5315306, 5315258, 5265134, 8673006, 5315306, 5315258, 5265134, 8673006,
5315258, 5527226, 5527226, 5528046, 5527277, 5528250, 5528057, 5527277, 5527277
];
// TODO ENCODED_MONTH_LENGTHS would be stored more efficiently converted to a string using
// String.fromCharCode.apply(String, ENCODED_MONTH_LENGTHS), and extracted using
// ENC_MTH.charCodeAt(...). However, JS seems to do something weird with the
// top bits.
/**
* Magic numbers:
* BS_YEAR_ZERO <- the first year (BS) encoded in ENCODED_MONTH_LENGTHS
* month #5 <- this is the only month which has a day variation of more than 1
* & 3 <- this is a 2 bit mask, i.e. 0...011
*/
function daysInMonth$1(year, month) {
if (month < 1 || month > 12)
throw new Error("Invalid month value " + month);
var delta = ENCODED_MONTH_LENGTHS[year - BS_YEAR_ZERO];
if (typeof delta === "undefined")
throw new Error("No data for year: " + year + " BS");
return 29 + ((delta >>> ((month - 1) << 1)) & 3);
}
function zPad(x) {
return x > 9 ? x : "0" + x;
}
function toBik(greg) {
// TODO do not use Date.parse(), as per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
const options = {
timeZone: "Asia/Kathmandu",
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric"
};
const timeInSeconds = new Date(new Date(greg).toLocaleString([], options));
var m, dM, year = BS_YEAR_ZERO, days = Math.floor((timeInSeconds - BS_EPOCH_TS) / MS_PER_DAY) + 1;
while (days > 0) {
for (m = 1; m <= 12; ++m) {
dM = daysInMonth$1(year, m);
if (days <= dM)
return { year: year, month: m, day: days };
days -= dM;
}
++year;
}
throw new Error("Date outside supported range: " + greg + " AD");
}
function toDev(year, month, day) {
return {
day: toDevanagari(day),
month: MONTH_NAMES[month - 1],
year: toDevanagari(year)
};
}
function toBik_euro(greg) {
var d = toBik(greg);
return d.year + "-" + zPad(d.month) + "-" + zPad(d.day);
}
function toBik_dev(greg) {
return toDevanagari(toBik_euro(greg));
}
function toBik_text(greg) {
var bik = toBik(greg);
var dev = toDev(bik.year, bik.month, bik.day);
return dev.day + " " + dev.month + " " + dev.year;
}
function toGreg(year, month, day) {
// TODO month bounds-checking should be handled in daysInMonth()
if (month < 1)
throw new Error("Invalid month value " + month);
if (year < BS_YEAR_ZERO)
throw new Error("Invalid year value " + year);
if (day < 1 || day > daysInMonth$1(year, month))
throw new Error("Invalid day value", day);
var timestamp = BS_EPOCH_TS + MS_PER_DAY * day;
month--;
while (year >= BS_YEAR_ZERO) {
while (month > 0) {
timestamp += MS_PER_DAY * daysInMonth$1(year, month);
month--;
}
month = 12;
year--;
}
var d = new Date(timestamp);
return {
year: d.getUTCFullYear(),
month: 1 + d.getUTCMonth(),
day: d.getUTCDate()
};
}
function toGreg_text(year, month, day) {
var d = toGreg(year, month, day);
return d.year + "-" + zPad(d.month) + "-" + zPad(d.day);
}
var bikramSambat = {
daysInMonth: daysInMonth$1,
toBik: toBik,
toDev: toDev,
toBik_dev: toBik_dev,
toBik_euro: toBik_euro,
toBik_text: toBik_text,
toGreg: toGreg,
toGreg_text: toGreg_text
};
const nepaliMonths = [
"बैशाख", // Baisakh
"जेठ", // Jestha
"असार", // Ashadh
"श्रावण", // Shrawan
"भदौ", // Bhadra
"आश्विन", // Ashwin
"कार्तिक", // Kartik
"मंसिर", // Mangsir
"पुष", // Poush
"माघ", // Magh
"फाल्गुण", // Falgun
"चैत्र" // Chaitra
];
const nepaliDays = [
"आइतबार", // Sunday
"सोमबार", // Monday
"मंगलबार", // Tuesday
"बुधबार", // Wednesday
"बिहीबार", // Thursday
"शुक्रबार", // Friday
"शनिबार" // Saturday
];
const yearOptions = [
{
label: "2000",
value: 2000
},
{
label: "2001",
value: 2001
},
{
label: "2002",
value: 2002
},
{
label: "2003",
value: 2003
},
{
label: "2004",
value: 2004
},
{
label: "2005",
value: 2005
},
{
label: "2006",
value: 2006
},
{
label: "2007",
value: 2007
},
{
label: "2008",
value: 2008
},
{
label: "2009",
value: 2009
},
{
label: "2010",
value: 2010
},
{
label: "2011",
value: 2011
},
{
label: "2012",
value: 2012
},
{
label: "2013",
value: 2013
},
{
label: "2014",
value: 2014
},
{
label: "2015",
value: 2015
},
{
label: "2016",
value: 2016
},
{
label: "2017",
value: 2017
},
{
label: "2018",
value: 2018
},
{
label: "2019",
value: 2019
},
{
label: "2020",
value: 2020
},
{
label: "2021",
value: 2021
},
{
label: "2022",
value: 2022
},
{
label: "2023",
value: 2023
},
{
label: "2024",
value: 2024
},
{
label: "2025",
value: 2025
},
{
label: "2026",
value: 2026
},
{
label: "2027",
value: 2027
},
{
label: "2028",
value: 2028
},
{
label: "2029",
value: 2029
},
{
label: "2030",
value: 2030
},
{
label: "2031",
value: 2031
},
{
label: "2032",
value: 2032
},
{
label: "2033",
value: 2033
},
{
label: "2034",
value: 2034
},
{
label: "2035",
value: 2035
},
{
label: "2036",
value: 2036
},
{
label: "2037",
value: 2037
},
{
label: "2038",
value: 2038
},
{
label: "2039",
value: 2039
},
{
label: "2040",
value: 2040
},
{
label: "2041",
value: 2041
},
{
label: "2042",
value: 2042
},
{
label: "2043",
value: 2043
},
{
label: "2044",
value: 2044
},
{
label: "2045",
value: 2045
},
{
label: "2046",
value: 2046
},
{
label: "2047",
value: 2047
},
{
label: "2048",
value: 2048
},
{
label: "2049",
value: 2049
},
{
label: "2050",
value: 2050
},
{
label: "2051",
value: 2051
},
{
label: "2052",
value: 2052
},
{
label: "2053",
value: 2053
},
{
label: "2054",
value: 2054
},
{
label: "2055",
value: 2055
},
{
label: "2056",
value: 2056
},
{
label: "2057",
value: 2057
},
{
label: "2058",
value: 2058
},
{
label: "2059",
value: 2059
},
{
label: "2060",
value: 2060
},
{
label: "2061",
value: 2061
},
{
label: "2062",
value: 2062
},
{
label: "2063",
value: 2063
},
{
label: "2064",
value: 2064
},
{
label: "2065",
value: 2065
},
{
label: "2066",
value: 2066
},
{
label: "2067",
value: 2067
},
{
label: "2068",
value: 2068
},
{
label: "2069",
value: 2069
},
{
label: "2070",
value: 2070
},
{
label: "2071",
value: 2071
},
{
label: "2072",
value: 2072
},
{
label: "2073",
value: 2073
},
{
label: "2074",
value: 2074
},
{
label: "2075",
value: 2075
},
{
label: "2076",
value: 2076
},
{
label: "2077",
value: 2077
},
{
label: "2078",
value: 2078
},
{
label: "2079",
value: 2079
},
{
label: "2080",
value: 2080
},
{
label: "2081",
value: 2081
},
{
label: "2082",
value: 2082
},
{
label: "2083",
value: 2083
},
{
label: "2084",
value: 2084
},
{
label: "2085",
value: 2085
},
{
label: "2086",
value: 2086
},
{
label: "2087",
value: 2087
},
{
label: "2088",
value: 2088
},
{
label: "2089",
value: 2089
}
];
const HolidaysFromJSON = {
"2082-01-01": {
bs_date: "1 Baishakh 2082",
holiday: ["Nepali New Year", "Mesha Sankranti"],
date: "2082-01-01"
},
"2082-01-05": {
bs_date: "5 Baishakh 2082",
holiday: ["Ram Navami"],
date: "2082-01-05"
},
"2082-01-18": {
bs_date: "18 Baishakh 2082",
holiday: ["International Workers’ Day"],
date: "2082-01-18"
},
"2082-01-24": {
bs_date: "24 Baishakh 2082",
holiday: ["Kirant Samaj Sudhar Diwas / Provincial Language Day"],
date: "2082-01-24"
},
"2082-01-29": {
bs_date: "29 Baishakh 2082",
holiday: ["Buddha Jayanti", "Ubhauli Parva"],
date: "2082-01-29"
},
"2082-02-15": {
bs_date: "15 Jestha 2082",
holiday: ["Republic Day", "National Paddy Day / Dahi Chiura Khane Din"],
date: "2082-02-15"
},
"2082-02-21": {
bs_date: "21 Jestha 2082",
holiday: ["National Day for Elimination of Discrimination"],
date: "2082-02-21"
},
"2082-02-24": {
bs_date: "24 Jestha 2082",
holiday: ["Nirjala Ekadashi"],
date: "2082-02-24"
},
"2082-02-30": {
bs_date: "30 Jestha 2082",
holiday: ["Siruwa Pawani Parva (Jhapa/Morang/etc)"],
date: "2082-02-30"
},
"2082-03-29": {
bs_date: "29 Asadh 2082",
holiday: ["Matatirtha Aunsi (Mother's Day)"],
date: "2082-03-29"
},
"2082-04-24": {
bs_date: "24 Shrawan 2082",
holiday: ["Janai Purnima", "Raksha Bandhan"],
date: "2082-04-24"
},
"2082-04-25": {
bs_date: "25 Shrawan 2082",
holiday: ["Gaijatra (Kathmandu Valley only)"],
date: "2082-04-25"
},
"2082-04-31": {
bs_date: "31 Shrawan 2082",
holiday: ["Krishna Janmashtami"],
date: "2082-04-31"
},
"2082-05-07": {
bs_date: "7 Bhadra 2082",
holiday: ["Kushe Aunsi (Father’s Day)"],
date: "2082-05-07"
},
"2082-05-10": {
bs_date: "10 Bhadra 2082",
holiday: ["Haritalika Teej (women’s holiday)"],
date: "2082-05-10"
},
"2082-05-15": {
bs_date: "15 Bhadra 2082",
holiday: ["Gaura Parva"],
date: "2082-05-15"
},
"2082-05-17": {
bs_date: "17 Bhadra 2082",
holiday: ["Rishi Panchami"],
date: "2082-05-17"
},
"2082-05-21": {
bs_date: "21 Bhadra 2082",
holiday: ["Indra Jatra (Kathmandu Valley only)"],
date: "2082-05-21"
},
"2082-05-22": {
bs_date: "22 Bhadra 2082",
holiday: ["Civil Service Day"],
date: "2082-05-22"
},
"2082-05-30": {
bs_date: "30 Bhadra 2082",
holiday: ["Jitiya Parva (women only)"],
date: "2082-05-30"
},
"2082-06-02": {
bs_date: "2 Ashwin 2082",
holiday: ["National Education Day"],
date: "2082-06-02"
},
"2082-06-03": {
bs_date: "3 Ashwin 2082",
holiday: ["Constitution Day"],
date: "2082-06-03"
},
"2082-06-06": {
bs_date: "6 Ashwin 2082",
holiday: ["Ghatasthapana"],
date: "2082-06-06"
},
"2082-06-13": {
bs_date: "13 Ashwin 2082",
holiday: ["Fulpati"],
date: "2082-06-13"
},
"2082-06-14": {
bs_date: "14 Ashwin 2082",
holiday: ["Maha Ashtami"],
date: "2082-06-14"
},
"2082-06-15": {
bs_date: "15 Ashwin 2082",
holiday: ["Maha Navami"],
date: "2082-06-15"
},
"2082-06-16": {
bs_date: "16 Ashwin 2082",
holiday: ["Bijaya Dashami"],
date: "2082-06-16"
},
"2082-07-02": {
bs_date: "2 Kartik 2082",
holiday: ["Kaag Tihar (start of Tihar)"],
date: "2082-07-02"
},
"2082-07-03": {
bs_date: "3 Kartik 2082",
holiday: ["Laxmi Puja / Narak Chaturdashi/ kukur tihar"],
date: "2082-07-03"
},
"2082-07-04": {
bs_date: "4 Kartik 2082",
holiday: ["Tihar Bida"],
date: "2082-07-04"
},
"2082-07-05": {
bs_date: "5 Kartik 2082",
holiday: ["Gai Tihar & Govardhan Puja"],
date: "2082-07-05"
},
"2082-07-06": {
bs_date: "6 Kartik 2082",
holiday: ["Bhai tika"],
date: "2082-07-06"
},
"2082-07-07": {
bs_date: "7 Kartik 2082",
holiday: ["Tihar bida"],
date: "2082-07-07"
},
"2082-07-10": {
bs_date: "10 Kartik 2082",
holiday: ["Chhath Parva"],
date: "2082-07-10"
},
"2082-08-18": {
bs_date: "18 Mangsir 2082",
holiday: ["Udhauli Parva / Yomari Punhi"],
date: "2082-08-18"
},
"2082-09-10": {
bs_date: "10 Poush 2082",
holiday: ["Christmas Day"],
date: "2082-09-10"
},
"2082-09-15": {
bs_date: "15 Poush 2082",
holiday: ["Tamu Lhosar", "Yomari Punhi"],
date: "2082-09-15"
},
"2082-09-27": {
bs_date: "27 Poush 2082",
holiday: ["Prithvi Jayanti"],
date: "2082-09-27"
},
"2082-10-01": {
bs_date: "1 Magh 2082",
holiday: ["Maghi Parva"],
date: "2082-10-01"
},
"2082-10-05": {
bs_date: "5 Magh 2082",
holiday: ["Sonam Lhosar"],
date: "2082-10-05"
},
"2082-10-09": {
bs_date: "9 Magh 2082",
holiday: ["Basant Panchami (educational only)"],
date: "2082-10-09"
},
"2082-10-16": {
bs_date: "16 Magh 2082",
holiday: ["Martyrs’ Day"],
date: "2082-10-16"
},
"2082-11-03": {
bs_date: "3 Falgun 2082",
holiday: ["Maha Shivaratri"],
date: "2082-11-03"
},
"2082-11-06": {
bs_date: "6 Falgun 2082",
holiday: ["Gyalpo Lhosar"],
date: "2082-11-06"
},
"2082-11-07": {
bs_date: "7 Falgun 2082",
holiday: ["Democracy Day"],
date: "2082-11-07"
},
"2082-11-18": {
bs_date: "18 Falgun 2082",
holiday: ["Fagu Purnima (in hilly districts)"],
date: "2082-11-18"
},
"2082-11-19": {
bs_date: "19 Falgun 2082",
holiday: ["Fagu Purnima (in Terai districts)"],
date: "2082-11-19"
},
"2082-11-24": {
bs_date: "24 Falgun 2082",
holiday: ["International Women’s Day"],
date: "2082-11-24"
},
"2082-12-04": {
bs_date: "4 Chaitra 2082",
holiday: ["Ghode Jatra (Kathmandu Valley only)"],
date: "2082-12-04"
},
"2082-12-13": {
bs_date: "13 Chaitra 2082",
holiday: ["Ram navami"],
date: "2082-12-13"
}
};
const options = {
timeZone: "Asia/Kathmandu",
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric"
};
const useCalendar = ({ year, month }) => {
const currentGregDate = new Date();
const currentTemoBikDate = bikramSambat.toBik(currentGregDate.toLocaleString([], options));
let currentBikDate;
if (year) {
if (!month)
currentBikDate = { year, month: 1, day: 1 };
else
currentBikDate = { year, month, day: 1 };
}
else {
if (month) {
currentBikDate = { ...currentTemoBikDate, month, day: 1 };
}
else
currentBikDate = currentTemoBikDate;
}
const currentBikDay = extractDayFromBikDate(currentBikDate);
const currentBikMonth = extractMonthFromBikDate(currentBikDate);
const currentBikYear = extractYearFromBikDate(currentBikDate);
const currentNoOfMonths = daysInMonth(currentBikYear, currentBikMonth);
const getThisMonths = () => {
return currentMonthDays(currentBikDate);
};
const getMonthDaysWithHolidays = () => {
return mapHolidays(currentBikDate);
};
const getToday = () => {
const currentDate = bikramSambat.toBik(new Date().toLocaleDateString());
return {
...currentTemoBikDate,
date: formatDate(`${currentDate.year}-${currentDate.month}-${currentDate.day}`)
};
};
const monthsWithHolidays = getMonthDaysWithHolidays();
return {
today: getToday(),
monthsWithHolidays,
getMonthDaysWithHolidays,
currentBikDay,
currentBikDate,
currentBikMonth,
currentNoOfMonths,
currentBikYear,
getThisMonths
};
};
const extractDayFromBikDate = (date) => {
return date["day"];
};
const extractMonthFromBikDate = (date) => {
return date["month"];
};
const extractYearFromBikDate = (date) => {
return date["year"];
};
const daysInMonth = (year, month) => {
return bikramSambat.daysInMonth(year, month);
};
const currentMonthDays = (date) => {
const gregDate = bikramSambat.toGreg(date.year, date.month, 1);
const currentMonthDate = new Date(gregDate.year, gregDate.month - 1, gregDate.day);
const todayWeekDay = currentMonthDate.getDay() % 7;
const currentMonthDays = daysInMonth(date.year, date.month);
const nextMonth = getNextMonth(date);
const prevMonth = getPrevMonth(date);
const totalDays = todayWeekDay + currentMonthDays;
const prevDays = new Array(todayWeekDay).fill(0).map((_, index) => {
return {
date: `${date.year}-${prevMonth.month}-${prevMonth.day - (todayWeekDay - 1 - index)}`,
month: prevMonth.month,
day: prevMonth.day - (todayWeekDay - 1 - index)
};
});
const thisMonthDays = new Array(currentMonthDays).fill(0).map((_, index) => {
return {
date: `${date.year}-${date.month}-${index + 1}`,
month: date.month,
day: index + 1
};
});
const nextMonthDays = new Array(42 - totalDays).fill(0).map((_, index) => {
return { date: `${date.year}-${nextMonth.month}-${index + 1}`, month: nextMonth.month, day: index + 1 };
});
const finalDays = [...prevDays, ...thisMonthDays, ...nextMonthDays];
return finalDays;
};
const getNextMonth = (date) => {
// console.log("second", nextMonthDate);
const nextMonthBikDate = {
year: date.month === 12 ? date.year + 1 : date.year,
month: date.month === 12 ? 1 : date.month + 1,
day: date.day
};
// console.log("third", nextMonthBikDate);
const totalDays = bikramSambat.daysInMonth(nextMonthBikDate.year, nextMonthBikDate.month);
// console.log("goru", bikramSambat.toBik(nextMonthDate));
const finalDate = { ...nextMonthBikDate, day: totalDays };
return finalDate;
};
const getPrevMonth = (date) => {
const prevMonthBikDate = {
year: date.month === 1 ? date.year - 1 : date.year,
month: date.month === 1 ? 12 : date.month - 1,
day: date.day
};
const totalDays = bikramSambat.daysInMonth(prevMonthBikDate.year, prevMonthBikDate.month);
const finalDate = { ...prevMonthBikDate, day: totalDays };
return finalDate;
};
const mapHolidays = (date) => {
const days = currentMonthDays(date);
const finalDays = days.map(item => {
const formattedDate = formatDate(item.date);
const holidayItem = HolidaysFromJSON[formattedDate];
if (holidayItem) {
return { ...item, ...holidayItem };
}
return { ...item, date: formattedDate };
});
return finalDays;
};
function formatDate(dateStr) {
const [year, month, day] = dateStr.split("-");
const paddedMonth = month.padStart(2, "0");
const paddedDay = day.padStart(2, "0");
return `${year}-${paddedMonth}-${paddedDay}`;
}
const getCurrentDate = () => {
const date = new Date().toLocaleString();
return bikramSambat.toBik(new Date(date));
};
export { HolidaysFromJSON, currentMonthDays, daysInMonth, extractDayFromBikDate, extractMonthFromBikDate, extractYearFromBikDate, getCurrentDate, getNextMonth, getPrevMonth, mapHolidays, nepaliDays, nepaliMonths, useCalendar, yearOptions };