moment series 1: use of add() method and subtract() method

At work, because I can’t find the answer I want when I go to search blogs, even after a blog, I haven’t found anything for a long time, and I feel tired, so I won’t talk nonsense here, just post the content Bar.

moment Chinese official website address: http://momentjs.cn

moment English address: https://momentjs.com

The function of the add method: It can calculate any time after any specified time (current or a given specific day), for example: it is 8:00 in the morning, 12:00 in a few hours, 8:20 in a few minutes, etc. and similar situations

The function of the subtract method: it is completely opposite to add. For example, it is 8 o'clock in the morning, what time was it three hours ago, what time was it 20 minutes ago, etc.

Because these two methods have opposite effects, so this article will not elaborate too much. The following add methods can be replaced by the subtract method

grammar:

moment().add(Number, String);

moment().add(Duration); // Duration is the duration, equivalent to add(Object) syntax

moment().add(Object);

Grammar example:


  moment().add(7, 'days');    // 常用语法

  var duration = moment.duration({'days' : 1});  
  moment().add(duration);

  moment().add({days:7,months:1});    // 综合语法
  等价与
  moment().add(7, 'days').add(1, 'months');

If you think it is too troublesome to write strings such as days, years, hours, etc., you can refer to the official website picture

insert image description here
The right side in the table is the abbreviation for the left side

Use Cases:

moment().add(7, 'days'); is equivalent to moment().add(7, 'd');

Note: This method only adds the parameters we wrote in the method, for example: we only increase the number of days, then the date obtained is only the number of days is different, and other values ​​​​will not change

Prior to version 2.8.0, the moment().add(String, Number) syntax was supported. It is now deprecated in favor of moment().add(Number, String) .

Since version 2.12.0, when passing decimals for days and months, they are rounded to the nearest whole number. Day of week, quarter, and year are converted to day or month and then rounded to the nearest integer.

The changes in version 2.12.0 may be a little confusing to understand, let’s understand it with a case


  moment().add(1.5, 'months') == moment().add(2, 'months') 

  moment().add(.7, 'years') == moment().add(8, 'months') // .7*12 = 8.4,取整到 8

Well, the add method of moment has come to an end here.
Finally, let’s add a little knowledge:
in our program,
the month starts from 0 and ends on 11. The month of 0 means that our actual week of January
starts from the week Day starts, so there is no Saturday, so the weekend returns 0

Validation method:
month: console.log('month',Moment().month());
week: console.log('day',Moment().add( x ,"d").day()); // x can be replaced by a desired number to verify that it is a weekend and is not 0

As for why this is so, it may be because the computer originated abroad, so the calculation of the date in the program is also based on their calculation method.

Guess you like

Origin blog.csdn.net/Smallwhitestrive/article/details/124999509