UNPKG

xe-utils

Version:

JavaScript 函数库、工具类

28 lines (25 loc) • 1.17 kB
import helperDateGetFullYear from './helperDateGetFullYear.js';import helperDateGetMonth from './helperDateGetMonth.js';import helperHasLeapYear from './helperHasLeapYear.js';import helperMathFloor from './helperMathFloor.js';import toStringDate from './toStringDate.js';import isValidDate from './isValidDate.js';// 月份天数常量表 var staticMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] /** * 返回某个月份的天数 * * @param {Date} date 日期或数字 * @param {Number} offset 月(默认当月)、前几个月、后几个月 * @return {Number} */ function getDayOfMonth (date, offsetNum) { date = toStringDate(date) if (isValidDate(date)) { var year = helperDateGetFullYear(date) var month = helperDateGetMonth(date) + (offsetNum || 0) // 规范化月份到 0-11 范围,并调整年份 if (month < 0 || month > 11) { year += helperMathFloor(month / 12) month = ((month % 12) + 12) % 12 } // 查表获取天数,闰年2月返回29 return month === 1 && helperHasLeapYear(year) ? 29 : staticMonthDays[month] } return NaN } export default getDayOfMonth