首页 > 解决方案 > 如何在 Typescript 中获取当前日期前 3 个月的日期对象

问题描述

我正在尝试从当前日期起 3 个月之前获取日期对象,就像这样。

toDate = new Date();
fromDate  = this.toDate.getMonth() - 3;

但这fromDate只是一个数字。每次我想从当前日期开始 3 个月之前获取日期。但这仅显示了这样的选项。

this.fromDate = (this.toDate.getMonth() - 3).toLocaleString()

是否有可能在 3 个月之前获得日期对象?

标签: typescriptdate

解决方案


也许我可以帮忙

toDate = new Date();
month = toDate.getMonth() -3;
newDate = new Date(toDate.setMonth(month));

推荐阅读