网站公告

本次更新:新公告样式

详细教程:vitepress.yiov.top

QQ 频道:******(无效二维码)

Skip to content

getRelativeDate 获取周、月、季度

更新: 1970/1/1 字数: 0 字 时长: 0 分钟

说明

getRelativeDate 是一个计算指定日期前后多少周、月或季度的新日期的函数。

参数描述
date基准日期,默认为当前日期,可以是 Date(JavaScript Date 对象)。
amount要前进或后退的数量,正数表示未来,负数表示过去。
unit时间单位,可选值包括:'周'、'月'、'季度'。

返回值

  • 返回一个新的 Date 对象,表示基准日期增加或减少指定单位数量后的新日期。

示例

js
import { getRelativeDate } from 'uviewos';

// 获取基准日期前后多少周、月或季度的新日期
const baseDate = new Date();

// 获取基准日期前2周的日期
const twoWeeksAgo = getRelativeDate(baseDate, -2, '周');
console.log(twoWeeksAgo); // 输出类似:"2024-08-24T00:00:00.000Z"

// 获取基准日期后3个月的日期
const threeMonthsLater = getRelativeDate(baseDate, 3, '月');
console.log(threeMonthsLater); // 输出类似:"2024-12-07T00:00:00.000Z"

// 获取基准日期前1个季度的日期
const oneQuarterAgo = getRelativeDate(baseDate, -1, '季度');
console.log(oneQuarterAgo); // 输出类似:"2024-06-07T00:00:00.000Z"

Released under the MIT License.

本站访客数 人次 本站总访问量