Calculate difference between two dates returns a negative number

前端 未结 4 1130
予麋鹿
予麋鹿 2021-01-15 05:12

I am trying to calculate how many days there are between different dates. Like the topic says, I\'m getting a negative value. I guess I could just take the absolute value, b

4条回答
  •  花落未央
    2021-01-15 06:10

    I guess I could just take the absolute value, but the fact that it returns a negative value, makes me doubt if the calculations are correct.

    Why? nextDay is earlier than previousDay, so of course subtracting previousDay will give a negative result.

    You're effectively asking "How many days do I need to add to previousDay in order to get to nextDay? The answer is -28.

    What does concern me is the names here - I'd expect a nextDay value to always be later than a previousDay value - so either the way you're finding those values is incorrect, or you need to change the names to make the meaning clearer. Presumably they're not "next" and "previous" in respect to the same context.

提交回复
热议问题