date and time
function ad2bs(adDate) {
// AD to BS conversion constants
const BS_START_YEAR = 2000;
const BS_START_MONTH = 9;
const BS_START_DAY = 17;
const AD_START_YEAR = 1943;
const AD_START_MONTH = 4;
const AD_START_DAY = 14;
const TOTAL_BS_MONTHS = 12;
const TOTAL_AD_MONTHS = 12;
const DAYS_IN_MONTH = [32, 32, 31, 32, 31, 31, 32, 32, 31, 30, 31, 30, 31];
const MONTH_NAMES = [
'Baishakh', 'Jestha', 'Ashadh', 'Shrawan', 'Bhadra', 'Ashwin',
'Kartik', 'Mangsir', 'Poush', 'Magh', 'Falgun', 'Chaitra'
];
// Calculate the number of days between the AD start date and the input AD date
const adDateObj = new Date(adDate);
const adYear = adDateObj.getFullYear();
const adMonth = adDateObj.getMonth() + 1;
const adDay = adDateObj.getDate();
const totalDaysInAD = Math.floor((adYear - AD_START_YEAR) * 365.25) +
Math.floor((adMonth - AD_START_MONTH) * 30.44) +
(adDay - AD_START_DAY);
// Calculate the number of days between the BS start date and the input AD date
const totalDaysInBS = totalDaysInAD + Math.floor((BS_START_YEAR - adYear) * 365.25) +
Math.floor((BS_START_MONTH - adMonth) * 30.44) +
(BS_START_DAY - adDay);
// Calculate the BS year, month, and day from the total number of days
let bsYear = BS_START_YEAR;
let bsMonth = BS_START_MONTH;
let bsDay = BS_START_DAY;
let remainingDays = totalDaysInBS;
while (remainingDays !== 0) {
// Calculate the number of days in the current BS month
const daysInMonth = DAYS_IN_MONTH[bsMonth - 1];
// If the remaining days are less than the number of days in the current month,
// we have found the BS year, month, and day corresponding to the input AD date
if (remainingDays < daysInMonth) {
bsDay += remainingDays;
break;
}
// Otherwise, subtract the number of days in the current month from the remaining days,
// and move on to the next BS month
remainingDays -= daysInMonth;
bsMonth++;
// If we have reached the end of the current BS year, move on to the next BS year
if (bsMonth > TOTAL_BS_MONTHS) {
bsYear++;
bsMonth = 1;
}
}
// Return the BS date as a string in the format "YYYY-MM-DD"
const bsDateStr = `${bsYear}-${bsMonth.toString().padStart(2, '0')}-${bsDay.toString().padStart(2, '0')}`;
return bsDateStr;
}
SEARCH
LATEST
3-latest-65px
ABOUT
Powered by Blogger.

No comments:
Post a Comment