How to calculate the current and future date and receive the difference in days

Asked

Viewed 279 times

-1

I’m using Moment.js to do date calculations, thinking about it I did this function below to make the current date calculation with the date of birth:

onCalcularData(date: any): number {
    return moment().diff(date, 'years');
}

However, I want to do the same thing I did in the function above, but now I want to calculate the difference in days, for example:

22/06/2020 - 26/06/2020 = 4

I tried using the "subtract" function but could not.

  • tries date.diff(Moment(), 'years');

1 answer

3


If you want the difference in days, just change to moment().diff(date, 'days').

Already about "calculate forward and not backward", you mean that instead of calculating data_atual - date, you want to date - data_atual?

If that’s the case, just reverse the signal:

onCalcularData(date: any): number {
    return -moment().diff(date, 'days');
}

I put the minus sign (-), what "reverses" the return. After all, if the date represent, for example, a date 10 days before the current date, the difference between them in days will always be 10, and the only thing that changes when reversing the order in which the subtraction is made will be the sign.


Of course you can also reverse the dates in the calculation:

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

The difference is I had to moment(date), for date was declared as any (i.e., it can be "anything"), so it will not necessarily be a moment. In doing moment(date), I guarantee she will be converted to a moment and I can use the method diff.

In your code that wasn’t necessary because according to the documentation, the method diff accepts "anything" as a parameter (a moment, one string, one Date, etc) and internally it converts it to moment, so you can pass the date directly to diff. But if you want to "reverse", you need to turn it into a moment.


Another alternative, if you want only the number of days between the dates, regardless of whether one date is in the future or in the past in relation to the other, may also:

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

Thus, whether the result is a positive or negative number, the function returns the number of days between the two dates without the sign (whether the difference is -10 or 10, the function will return 10).

But if the sign is important, you should choose the order that makes the most sense for your case, and not use Math.abs.


But there’s a detail there. The calculation also takes into account the time, and the difference in days is rounded down (for example, between 10h today and 9h tomorrow, the difference is zero days - only from 10h tomorrow is that the difference gives 1). And the current date (moment()) will always have the time when she was created.

One way to skip the time is to set both dates to the same time. An alternative is to use startOf('day') to set them for the start of the day:

onCalcularData(date: any): number {
    return Math.abs(moment().startOf('day').diff(moment(date).startOf('day'), 'days'));
}
  • Sorry for the confusing text, about your suggested code "Moment(). diff(date, 'days')", I’m thinking it would fit. What I want to do is: today is the 22nd and in my field date, I place the 26th- I want to calculate this difference, ie 4 days

  • @Fernanda Then just combine the 2 suggestions, invert the dates and change to days: moment(date).diff(moment(), 'days')

  • @Fernanda I updated the answer with this option

  • @hjotsubo, I did it this way, but he doesn’t give me an exact calculation. He took the current date, plus the scheduled date and gave me the days between a date and another, example: 22/06 - 24/06 = 1. I would like him to return me 22/06 - 24/06 = 2 or 3

  • @Fernanda I updated the answer again

  • I think I understand, thank you very much

Show 1 more comment

Browser other questions tagged

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