尝试设置时间时Moment JS Deprecation警告

Moment JS Deprecation Warning when trying to set time

本文关键字:JS Deprecation 警告 Moment 设置 时间      更新时间:2023-09-26

如果我试图使用以下方法之一将时间设置为午夜,请使用即时版本2.9

    var date = moment('2015-03-28T10:55:10.050');
    date = moment({hour: 0, minute: 0, seconds: 0, milliseconds: 0});

    date.hours(0).minutes(0).seconds(0).milliseconds(0); 

    date.set('hours',0).set('minutes',0).set('seconds',0).set('milliseconds',0);

我得到

    'Deprecation warning: moment().add(period, number) is deprecated. Please use moment().add(number, period).'

那么我做错了什么?

更新:

只是为了提供进一步的信息,这可能会有所帮助,当我运行Karma Jasmine单元测试时,控制台中会出现警告,它们不会出现在web应用程序的控制台日志中,只有在我进行单元测试时才会出现。

您应该使用.startOf('day')

请记住,午夜并不总是存在于每个时区的每一天。一些时区在午夜开始夏令时,所以对于这些时区来说,当天的开始时间是1:00。矩用startOf函数说明了这一点。