The code 'moment().sub' is incomplete and will result in a syntax error. In order to calculate a duration or subtract a duration from a moment, you need to provide the duration as an argument to the 'sub' method.

Here is an example of how you can subtract a duration from a moment using the 'subtract' method:

const moment = require('moment');

const start = moment('2021-01-01');
const duration = moment.duration(1, 'days');

const end = start.subtract(duration);

console.log(end.format('YYYY-MM-DD')); // Output: 2020-12-31

In this example, we subtract a duration of 1 day from the moment 'start' using the 'subtract' method. The resulting moment is stored in the 'end' variable. Finally, we format the 'end' moment as a string using the 'format' method and log it to the console.

Moment.js subtract() Method: Subtracting Durations from Moments

原文地址: https://www.cveoy.top/t/topic/pdvq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录