ad-bs-date-conversion
Version:
299 lines (287 loc) • 14.4 kB
JavaScript
export const calendar_data = {
'1970': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1971': [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30, 365],
'1972': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'1973': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'1974': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1975': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'1976': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'1977': [30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365],
'1978': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1979': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'1980': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'1981': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'1982': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1983': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'1984': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'1985': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'1986': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1987': [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'1988': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'1989': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1990': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1991': [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'1992': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'1993': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1994': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1995': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'1996': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'1997': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1998': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'1999': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2000': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'2001': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2002': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2003': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2004': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'2005': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2006': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2007': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2008': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365],
'2009': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2010': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2011': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2012': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2013': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2014': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2015': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2016': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2017': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2018': [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2019': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'2020': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2021': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2022': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'2023': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'2024': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2025': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2026': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2027': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'2028': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2029': [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30, 365],
'2030': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2031': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'2032': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2033': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2034': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2035': [30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365],
'2036': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2037': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2038': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2039': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2040': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2041': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2042': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2043': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2044': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2045': [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2046': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2047': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2048': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2049': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'2050': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'2051': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2052': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2053': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'2054': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'2055': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2056': [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30, 365],
'2057': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2058': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365],
'2059': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2060': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2061': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2062': [30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31, 365],
'2063': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2064': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2065': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2066': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365],
'2067': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2068': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2069': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2070': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2071': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2072': [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2073': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2074': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2075': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2076': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'2077': [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366],
'2078': [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2079': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2080': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365],
'2081': [31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366],
'2082': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2083': [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2084': [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2085': [31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30, 366],
'2086': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2087': [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30, 366],
'2088': [30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30, 365],
'2089': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2090': [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365],
'2091': [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30, 366],
'2092': [31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366],
'2093': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365],
'2094': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2095': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
'2096': [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366],
'2097': [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365],
'2098': [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365],
'2099': [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365],
};
const getSeparatedDateFromBsDate = (BsDate) => {
let splittedDate = ["-", "-", "-"];
if (BsDate) {
splittedDate = BsDate.split("-");
}
return {
day: splittedDate[2],
month: splittedDate[1],
year: splittedDate[0]
}
}
export const getDateMilliseconds = (date, dateType) => {
dateType = dateType || "BS";
if (dateType === "BS") {
date = BSToAD(date);
}
return new Date(date).getTime();
}
const findPassedDaysInThatYear = (year, month, day) => {
let totalDays = 0;
for (let i = 0; i < month - 1; i++) {
totalDays += calendar_data[year][i];
}
if (day <= calendar_data[year][month - 1]) {
totalDays += Number(day);
} else {
totalDays += calendar_data[year][month - 1];
}
return totalDays;
}
const getMilliDifference = (date) => {
let dayDiff = 0;
let dateObject = getSeparatedDateFromBsDate(date);
let year = dateObject.year;
let month = dateObject.month;
let day = dateObject.day;
let passedDays = findPassedDaysInThatYear(year, month, day);
if (year > 2026) {
for (let i = 2027; i < year; i++) {
dayDiff += calendar_data[i][12];
}
dayDiff += (passedDays + 102);
} else if (year < 2026) {
for (let i = 2025; i > year; i--) {
dayDiff -= calendar_data[i][12];
}
dayDiff -= ((calendar_data[year][12] - passedDays) + 264);
} else {
dayDiff += (passedDays - 264);
}
return (dayDiff * 24 * 60 * 60 * 1000);
}
export const BSToAD = (date) => {
date = date.replaceAll("/", "-");
let dateData = date.split('-').map(d => parseInt(d, 10));
if ((dateData.length < 3) || !((dateData[0] > 1969) && dateData[0] < 2100) || !((dateData[1] > 0) && (dateData[1] < 13)) || !((dateData[2] > 0) && (dateData[2] <= calendar_data[dateData[0]][dateData[1] - 1]))) {
if (!!date) {
dateData.length < 3 ? alert("मान्य ढाँचामा नभएको मिति परिणत हुने छैन") : dateData[0] < 1970 ? alert("१९७० साल भन्दा कम मिति परिणत हुने छैन") : dateData[0] > 2099 ? alert("२०९९ साल भन्दा बढी मिति परिणत हुने छैन") : alert("पात्रोमा नभएको मिति परिणत हुने छैन");
}
return null;
}
let dateObject = new Date(getMilliDifference(date));
var year = dateObject.getFullYear();
var month = ("0" + (dateObject.getMonth() + 1)).slice(-2);
var day = ("0" + dateObject.getDate()).slice(-2);
return year + "-" + month + "-" + day;
}
const findBsDateFromTotalNumberOfDays = (year, days) => {
let month = 0;
for (let i = 0; i < 12; i++) {
if (days <= calendar_data[year][i]) {
month = i + 1;
break;
} else {
days -= calendar_data[year][i];
}
}
return {
bsYear: year,
bsMonth: month,
bsDay: days
}
}
export const findLastDayOfMonthOfAdDate = (year, month) => {
let nextMonth = Number(month) + 1;
if (nextMonth > 12) {
nextMonth = 1;
year = Number(year) + 1;
}
let milliDate = (new Date(year + "-" + ("0" + nextMonth).slice(-2) + "-" + "01").getTime()) - (24 * 60 * 60 * 1000);
return (new Date(milliDate).getDate());
}
export const findLastDayOfMonthOfBsDate = (year, month) => {
year = Number(year);
month = Number(month);
if ((year < 1970 && year > 2099) || (month > 0 && month < 13)){
return calendar_data[year][month - 1];
}
return null;
}
export const ADToBS = (date) => {
date = date.replaceAll("/", "-");
let separatedDate = date.split("-");
if ((separatedDate.length < 3) || !((separatedDate[1] > 0) && (separatedDate[1] < 13)) || !((separatedDate[2] > 0) && (separatedDate[2] <= findLastDayOfMonthOfAdDate(separatedDate[0], separatedDate[1])))) {
if (!!date) {
separatedDate.length < 3 ? alert("मान्य ढाँचामा नभएको मिति परिणत हुने छैन") : alert("पात्रोमा नभएको मिति परिणत हुने छैन");
}
return null;
}
date = separatedDate[0] + "-" + ("0" + separatedDate[1]).slice(-2) + "-" + ("0" + separatedDate[2]).slice(-2);
let dateInMillisecond = new Date(date).getTime();
if (dateInMillisecond >= -1789948800000 && dateInMillisecond <= 2312582400000) {
let equivalentDay = dateInMillisecond / (24 * 60 * 60 * 1000);
let year = 2026;
let currentYear = 0;
let currentMonth = 0;
let currentDay = 0;
let bsDateObject;
if (equivalentDay > 102) {
equivalentDay -= 102;
while (equivalentDay > 0) {
equivalentDay -= calendar_data[year + 1][12];
if (equivalentDay <= 0) {
bsDateObject = findBsDateFromTotalNumberOfDays(year + 1, (equivalentDay + calendar_data[year + 1][12]));
} else {
year++;
}
}
} else if (equivalentDay < -264) {
equivalentDay = -equivalentDay;
equivalentDay -= 264;
while (equivalentDay > 0) {
equivalentDay -= calendar_data[year - 1][12];
if (equivalentDay <= 0) {
if (equivalentDay === 0) {
bsDateObject = findBsDateFromTotalNumberOfDays(year - 2, calendar_data[year - 2][12]);
} else {
bsDateObject = findBsDateFromTotalNumberOfDays(year - 1, (-equivalentDay));
}
} else {
year--;
}
}
} else {
bsDateObject = findBsDateFromTotalNumberOfDays(year, (264 + Number(equivalentDay)));
}
currentDay = bsDateObject.bsDay;
currentMonth = bsDateObject.bsMonth;
currentYear = bsDateObject.bsYear;
return currentYear + "-" + ("0" + currentMonth).slice(-2) + "-" + ("0" + currentDay).slice(-2);
} else {
alert("परिणत गर्न खोजिएको मिति हाम्रो पात्रोमा अवस्थित छैन");
return null;
}
}