UNPKG

xe-utils

Version:

JavaScript 函数库、工具类

37 lines (30 loc) • 1.29 kB
import staticStrFirst from './staticStrFirst.js';import staticStrLast from './staticStrLast.js';import staticParseInt from './staticParseInt.js';import helperDateGetFullYear from './helperDateGetFullYear.js';import getWhatMonth from './getWhatMonth.js';import toStringDate from './toStringDate.js';import isValidDate from './isValidDate.js';/** * 返回前几年或后几年的日期 * * @param {Date} date 日期或数字 * @param {Number} offset 年(默认当前年)、前几个年(数值)、后几个年(数值) * @param {Number/String} month 获取哪月(null默认当前年)、年初(first)、年末(last)、指定月份(0-11) * @return {Date} */ function getWhatYear (date, offset, month) { date = toStringDate(date) if (!isValidDate(date)) { return date } if (offset != null) { var number = staticParseInt(offset) || 0 date.setFullYear(helperDateGetFullYear(date) + number) } if (month === staticStrFirst) { return new Date(helperDateGetFullYear(date), 0, 1) } if (month === staticStrLast) { date.setMonth(11) return getWhatMonth(date, 0, staticStrLast) } if (month != null && !isNaN(month)) { date.setMonth(staticParseInt(month)) } return date } export default getWhatYear