如何使用moment js获取18年前的信息[已关闭]

问题描述 投票:0回答:1

所以我知道我可以使用

moment().substract(18, 'years')
来获取 18 年前的日期。对于我的日期时间选择器

问题是,它给了我这样的输出:
今天是 2023 年 9 月 5 日。使用

moment().substract(18, 'years')

我得到 2005 年 9 月 5 日

你可以猜到,我只需要减去它的年份。您可以在下图中看到,由于该减法功能,我无法选择大于 2005 年 9 月 5 日 的日期。

有什么办法可以解决这个问题吗?我只需要减去它的年份,所以我想让2005年9月剩下的时间都可以被选择,不限于9月5日。

我尝试将

moment().year()-18
作为 maxDate 属性,但它当然不起作用。

javascript reactjs typescript momentjs
1个回答
-1
投票

可以使用减法

const moment = require('moment');

// Get the current date
const currentDate = moment();

// Subtract 18 years from the current date
const dateFrom18YearsAgo = currentDate.subtract(18, 'years')
© www.soinside.com 2019 - 2024. All rights reserved.