lunisolar
Version:
专业农历库,支持公历阴历互转,支持各类黄历数据查询,如八字四柱、阴历、神煞宜忌、时辰吉凶、建除十二神、胎神占方、五行纳音等。支持自定义插件。
25 lines (23 loc) • 853 B
text/typescript
import { UNITS } from '../constants'
import { parseDate, prettyUnit } from './index'
export function dateAdd(date: DateParamType, value: number, unit?: DateAddUnit): Date {
date = parseDate(date)
const year = date.getFullYear()
const month = date.getMonth() + 1
unit = (unit ? prettyUnit(unit) : 'millisecond') as DateAddUnitFullName
let diff = value
if (unit === UNITS.d || unit === UNITS.ld) {
diff = value * 24 * 60 * 60 * 1000
} else if (unit === UNITS.h) {
diff = value * 60 * 60 * 1000
} else if (unit === UNITS.m) {
diff = value * 60 * 1000
} else if (unit === UNITS.s) {
diff = value * 1000
} else if (unit === UNITS.M) {
return new Date(date.setMonth(month - 1 + value))
} else if (unit === UNITS.y) {
return new Date(date.setFullYear(year + value))
}
return new Date(date.valueOf() + diff)
}