gregorian-ethiopian-datepicker
Version:
A React date picker component supporting both Gregorian and Ethiopian calendars
92 lines (91 loc) • 3.08 kB
JavaScript
;
/**
* Gregorian calendar utility functions
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentGregorianDate = exports.getGregorianMonthName = exports.isValidGregorianDate = exports.jdnToGregorian = exports.gregorianToJDN = exports.getGregorianMonthDays = exports.isGregorianLeapYear = void 0;
// Gregorian calendar constants
var GREGORIAN_EPOCH = 1721425.5; // Julian Day Number for January 1, 1 A.D.
var GREGORIAN_MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var GREGORIAN_MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
/**
* Checks if a given Gregorian year is a leap year
*/
var isGregorianLeapYear = function (year) {
return (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0));
};
exports.isGregorianLeapYear = isGregorianLeapYear;
/**
* Gets the number of days in a Gregorian month
*/
var getGregorianMonthDays = function (year, month) {
if (month < 1 || month > 12)
return 0;
if (month === 2)
return (0, exports.isGregorianLeapYear)(year) ? 29 : 28;
return GREGORIAN_MONTH_DAYS[month - 1];
};
exports.getGregorianMonthDays = getGregorianMonthDays;
/**
* Converts Gregorian date to Julian Day Number
*/
var gregorianToJDN = function (year, month, day) {
var a = Math.floor((14 - month) / 12);
var y = year + 4800 - a;
var m = month + 12 * a - 3;
return day +
Math.floor((153 * m + 2) / 5) +
365 * y +
Math.floor(y / 4) -
Math.floor(y / 100) +
Math.floor(y / 400) -
32045;
};
exports.gregorianToJDN = gregorianToJDN;
/**
* Converts Julian Day Number to Gregorian date
*/
var jdnToGregorian = function (jdn) {
var f = jdn + 1401 + Math.floor((Math.floor((4 * jdn + 274277) / 146097) * 3) / 4) - 38;
var e = 4 * f + 3;
var g = Math.floor((e % 1461) / 4);
var h = 5 * g + 2;
var day = Math.floor((h % 153) / 5) + 1;
var month = (Math.floor(h / 153) + 2) % 12 + 1;
var year = Math.floor(e / 1461) - 4716 + Math.floor((14 - month) / 12);
return [year, month, day];
};
exports.jdnToGregorian = jdnToGregorian;
/**
* Validates a Gregorian date
*/
var isValidGregorianDate = function (year, month, day) {
if (year < 1)
return false;
if (month < 1 || month > 12)
return false;
if (day < 1 || day > (0, exports.getGregorianMonthDays)(year, month))
return false;
return true;
};
exports.isValidGregorianDate = isValidGregorianDate;
/**
* Gets the name of a Gregorian month
*/
var getGregorianMonthName = function (month) {
if (month < 1 || month > 12)
return '';
return GREGORIAN_MONTH_NAMES[month - 1];
};
exports.getGregorianMonthName = getGregorianMonthName;
/**
* Gets today's date in Gregorian calendar
*/
var getCurrentGregorianDate = function () {
var today = new Date();
return [today.getFullYear(), today.getMonth() + 1, today.getDate()];
};
exports.getCurrentGregorianDate = getCurrentGregorianDate;