Jin Huh
Jin Huh

Reputation: 49

How to change the date using Moment.js?

I tried to change the date using moment JS. How do I make data child[index-1].childEndDate = childStartDate -1 ?

For Example:

if child[1].childEndDate = 03/22/2019,
child[0].childStartDate = 03/21/2019 

child[index-1].childEndDate = childStartDate -1 doesn't work. I think childStartDate - 1 is not correct to use with MomentJS

childAutoChangeData(start, end, child, index) {
    const childStartDate = moment(start);
    const childEndDate = moment(end);

    if (index > 0 && !end.isSame(child[index].childStartDate + 1)) {
        child[index - 1].childEndDate = start-1;
    }

    return child;
}

Upvotes: 0

Views: 81

Answers (1)

Sagar Chaudhary
Sagar Chaudhary

Reputation: 1403

Date cannot be subtracted like numbers.

Based on Moment Docs, you can use .subtract(). It accepts a number and a string such as 1 and days.

So do this instead:

child[index-1].childEndDate = moment(childStartDate)
                                  .subtract(1, "days")
                                  .format("MM/DD/YYYY");

Upvotes: 6

Related Questions