How to add days to a date with Moment.js?

Asked

Viewed 835 times

3

I’m trying to calculate the deadline of my site in days, IE, I put "14 days" and it turns into 00/00/0000.

I’m doing it this way, but she’s taking the full date and turning it into days, I want to do it backwards, for example:

Current date: 08/07/2020 + Input (input value): 14 = expected exit 22/07/2020.

onCalcularData(date: any): number {
    return Math.abs(
      moment()
        .startOf('day')
        .diff(moment(date).startOf('day'), 'days'),
    );
  }

How to get to this?

  • 1

    Your question is confused. Give an example of input, expected output and current output.

  • 1

    Oi Rafael - following your proposal, I need to take the current date and fill in an input a decimal value, for example: Current date: 08/07/2020 + Input (input value): 14 = expected output 22/07/2020. Today I calculate by taking two dates and turning them into days

1 answer

5


To add a certain amount of days to a date, just use add:

onCalcularData(date: any, dias: number): moment.Moment {
    return moment(date).add(dias, 'days');
}

The return will be a Moment. But if you want to return one Date, just use toDate():

onCalcularData(date: any, dias: number): Date {
    return moment(date).add(dias, 'days').toDate();
}

So, if you run with the current date (08/07/2020) and add 14 days (onCalcularData(new Date(), 14)), the result will be 22/07/2020. Remembering that the time will be kept (for example, it is now 8:12 in the morning, so the final result will be 22/07 at the same time).


Taking advantage, I think it is worth explaining two important concepts: dates and durations.

A date is a specific point in the calendar. For example, 08/07/2020 represents the 8th day of month 7 of year 2020 of the Gregorian calendar.

A duration is a amount of time. For example, "14 days" - represents only a period, any amount of time, but without any relation to the calendar (the duration exists by itself, if I say only "14 days", it is not possible to know when it started or ended).

What can be confusing is the fact that they both use the same words ("day", "month", etc), but they are different things.

But these two concepts are related. The difference between two dates is a duration (between days 8 and 22, the difference - or the duration between those dates - is 14 days).

And if I add a date with a duration, the result is another date (day 8 + duration 14 days = day 22).

The question code uses diff, that calculates the difference between two dates and returns a duration. But you wanted to add a duration and a date, and that’s why add serves.


Complementing, if the idea is to add at all times from the current date, it would look like this:

onCalcularData(dias: number): moment.Moment {
    return moment().add(dias, 'days');
}
  • What would it be like to run with Newdate? I’m a layperson in programming

  • @Fernanda The way I did, you pass the date as parameter: onCalcularData(new Date(), 14). Now, if you always want to add up to 14 days on the current date, then the function would just be onCalcularData(dias: number) { return moment().add(dias, 'days') }

  • i tried to do so onCalcularData(newDate(), days: number), because the days will be the user’s discretion, but I get an error of the angular component, so the question

  • @Fernanda new Date() has a space after "new". Anyway, if you always want to add on the current date, you do not need to pass it as a parameter to the function. I updated the answer with this option

Browser other questions tagged

You are not signed in. Login or sign up in order to post.