1

Using moment version 2.9 if I try to set a time to midnight using either of the following methods

    var date = moment('2015-03-28T10:55:10.050');

    date = moment({hour: 0, minute: 0, seconds: 0, milliseconds: 0});

or

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

or

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

I get

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

So what am I doing wrong?

UPDATE:

Just to give further info, which might help, the warning is coming up in the console when I run Karma Jasmine unit tests, they don't appear in the console log in the web app, these only appear when I'm unit testing.

DanielST
  • 13,783
  • 7
  • 42
  • 65
BrokenEyes
  • 192
  • 2
  • 18
  • Are you sure you are using 2.9? Double check by logging `moment.version`. I can't reproduce it. – DanielST Mar 26 '15 at 18:43
  • That is what is says at the start of the moment.js file. I also console.logged moment.version and it says 2.9.0 – BrokenEyes Mar 27 '15 at 08:53
  • 1
    If it's just the unit tester that's spitting out deprecation warnings, I'd likely ignore it. What you are doing with moment is certainly not deprecated. The deprecation that it is mentioning is at the bottom of [Add] in the docs (http://momentjs.com/docs/#/manipulating/add/) – DanielST Mar 27 '15 at 13:30
  • Thank you, just confused because "I'm" not doing an "add" so my only assumption is that its some internal code spitting out the deprecation message, – BrokenEyes Mar 27 '15 at 15:00
  • Also my first two method were incorrect for what I actually wanted to do, because they set the date to 'today' at midnight and I actually wanted a 'given' date to be set to midnight in which case the 'set' method is the one to use, just in case others come across this code, I don't want to give out wrong coding ;) – BrokenEyes Mar 27 '15 at 15:02
  • You should be using `startOf` as per Matt's answer. If it isn't working, you should figure out why. – DanielST Mar 27 '15 at 15:09

1 Answers1

1

You should be using .startOf('day')

Keep in mind that midnight doesn't always exist for every day in every time zone. Some time zones spring forward for daylight saving time right at midnight, so for these zones the start of that day would be 1:00. Moment accounts for this with the startOf function.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • This doesn't seem to work for me, given a date time of 2015-03-28T22:55:10.050 this gives me midnight on the 29th, but I want midnight of that day, not the next. – BrokenEyes Mar 27 '15 at 08:54
  • and I still get the deprecation warning with that, so something isn't right. – BrokenEyes Mar 27 '15 at 09:02
  • 1
    `moment('2015-03-28T22:55:10.050').startOf('day').format()` gives me `"2015-03-28T00:00:00-07:00"`. I'm in the Pacific time zone. It will give whatever the local time zone offset is for that date, but it should not be changing the date itself. Perhaps something you are doing differently in how you load the moment? – Matt Johnson-Pint Mar 27 '15 at 15:06
  • I get no deprecation warnings at all with that. I think perhaps you are getting the warnings from somewhere else in your code. – Matt Johnson-Pint Mar 27 '15 at 15:07
  • 2
    As a sanity check, just go to momentjs.com and open a debug console in your browser there. Moment is already loaded, so you can just test right on the site that way. – Matt Johnson-Pint Mar 27 '15 at 15:08