需要以角度7传递当前日期的最后一天

时间:2019-04-02 12:33:50

标签: angular

我正在尝试将日期以短日期格式传递给我的角度应用程序中的方法,但是日期必须是月份的最后一天。我该怎么做

这就是我所做的

import {formatDate} from '@angular/common';

 this.allocationsComponent.getAllocationsDetails(formatDate(new Date(), 'yyyy/MM/dd', 'en'));

结果是

2019/04/02

3 个答案:

答案 0 :(得分:2)

尝试一下:

var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
this.allocationsComponent.getAllocationsDetails(formatDate(lastDayOfMonth, 'yyyy/MM/dd', 'en'));

答案 1 :(得分:1)

JavaScript的Date有一个有趣的怪癖:一个月的第0天是上个月的最后一天:

> var d=new Date();
Tue Apr 02 2019 14:40:02
> d.setDate(0);
Sun Mar 31 2019 14:40:02

答案 2 :(得分:0)

简单方法和简化/单行代码

this.allocationsComponent.getAllocationsDetails(formatDate(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0)), 'yyyy/MM/dd', 'en'));
相关问题