如何使用Angular和Moment获得下个月的第一天

时间:2019-06-05 05:30:47

标签: angular momentjs

我们在应用程序上生成的日志存在问题。解决方案是,即使选定的日期在该月中旬,我们也需要获取下个月的第一天。

试图阅读有关Moment的文档,但我找不到任何可能的解决方案。

var currentDate = new Date();
var start_date = new Date(); //Wed Jun 05 2019 13:23:06 GMT+0800 (Philippine Standard Time)

console.log(start_date);

start_date.setDate(start_date.getDate() - 365); //1496640186958

console.log(start_date.setDate(start_date.getDate() - 365)); //Monday, June 5, 2017 1:23:06.958 PM GMT+08:00
// this output should be July 1, 2017, how can I get the first day of next month here?

start_date = customizeDate.formatDateToString(start_date);
var end_date = dateRange.endDate;

4 个答案:

答案 0 :(得分:2)

您可以将其与自定义格式一起使用

=B2&", "&D2

=IF(COUNTIF(E:E,E2)>1,"exists","unique")
const startOfNextMonth = moment().add(1, 'M').startOf('month').format('YYYY-MM-DD hh:mm:ss');

答案 1 :(得分:0)

好的,我设法使用以下功能链解决了这个问题:

console.log(moment(1496640186958).add(1, 'months').startOf('month').format('ll'));

答案 2 :(得分:0)

这是一种可能的方法,创建一个新的日期对象,然后在月份中添加1,最后将日期设置为第一天:

start_date = new Date(start_date.getFullYear(), start_date.getMonth() + 1, 1);

Stackblitz

答案 3 :(得分:-1)

您可以使用Vanilla JavaScript来实现。请注意,我添加了if语句来检查当前日期的月份是否为12月。这是为了防止出现任何问题,导致月份“溢出”到12。

const today = new Date(); 
let next;
if (today.getMonth() === 11) {
  next = new Date(today.getFullYear() + 1, 0, 1);
} else {
    next = new Date(today.getFullYear(), today.getMonth() + 1, 1);
}

console.log(next);