UNPKG

lunar-lite

Version:

精简版的农历和阳历日期转换库。

58 lines (57 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTotalDaysOfSolarMonth = exports.getTotalDaysOfLunarMonth = exports.getTotalDaysOfLunarYear = void 0; var leap_1 = require("./leap"); var constants_1 = require("./constants"); /** * 返回农历年一整年的总天数 * * @param year 农历年份 * @return number * @example * count = getTotalDaysOfLunarYear(1987) ;//count=387 */ var getTotalDaysOfLunarYear = function (year) { var sum = 348; for (var i = 0x8000; i > 0x8; i >>= 1) { sum += constants_1.LUNAR_INFO[year - 1900] & i ? 1 : 0; } return sum + (0, leap_1.getLeapDays)(year); }; exports.getTotalDaysOfLunarYear = getTotalDaysOfLunarYear; /** * 返回农历月(非闰月)的总天数,计算闰月时的天数请使用getLeapDays方法 * * @param year 农历年份 * @param month 农历月份,取值【1~12】 * @return 29 | 30 * @example * MonthDay = getTotalDaysOfLunarMonth(1987,9) ;//MonthDay=29 */ var getTotalDaysOfLunarMonth = function (year, month) { if (month > 12 || month < 1) { throw new Error("invalid month."); } return constants_1.LUNAR_INFO[year - 1900] & (0x10000 >> month) ? 30 : 29; }; exports.getTotalDaysOfLunarMonth = getTotalDaysOfLunarMonth; /** * 返回公历月的天数 * * @param year 公历年 * @param month 公历月,取值【1~12】,若参数错误 返回-1 * @return 28 | 29 | 30 | 31 * @example * solarMonthDay = getTotalDaysOfSolarMonth(1987, 1) ; // solarMonthDay=30 */ var getTotalDaysOfSolarMonth = function (year, month) { if (month > 12 || month < 1) { throw new Error("invalid month."); } if (month === 2) { // 2月份的闰平规律测算后确认返回28或29 return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ? 29 : 28; } return constants_1.SOLAR_MONTH[month - 1]; }; exports.getTotalDaysOfSolarMonth = getTotalDaysOfSolarMonth;