力矩:如何获得日期范围的月份阵列

发布于 2025-02-01 18:20:35 字数 202 浏览 2 评论 0原文

let startDate = '2022/01/1';
let endDate = '2022/05/31'

力矩是否具有从上面给出日期范围的下一个月阵列获得的功能?

[
  2022/01, 2022/02, 2022/03, 2022/04, 2022/05
]

多谢。

let startDate = '2022/01/1';
let endDate = '2022/05/31'

Does moment have the function to get the following month array from the above give date range??

[
  2022/01, 2022/02, 2022/03, 2022/04, 2022/05
]

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2025-02-08 18:20:35

您可以使用 添加方法来实现这一目标:

const monthsBetweenDates = (startDate, endDate) => {
    const now = startDate, dates = [];
      
    while (now.isSameOrBefore(endDate)) {
          dates.push(now.format('YYYY/MM'));
          now.add(1, 'months');
    }
    return dates;
}

const fromDate = moment();
const toDate   = moment().add(6, 'months');
const results = monthsBetweenDates(fromDate, toDate);
console.log(results);

You can use isSameOrBefore with add methods to achieve this:

const monthsBetweenDates = (startDate, endDate) => {
    const now = startDate, dates = [];
      
    while (now.isSameOrBefore(endDate)) {
          dates.push(now.format('YYYY/MM'));
          now.add(1, 'months');
    }
    return dates;
}

const fromDate = moment();
const toDate   = moment().add(6, 'months');
const results = monthsBetweenDates(fromDate, toDate);
console.log(results);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文