0

about first question,let's say there are two data: "2019-2-15",'2020-4-5', first I want to know how many days between those 2 data. and I need to know how many days for each month(consider leap year,leap month,solar month,lunar month)

so in this case: "2019-2-15"~'2020-4-5', there should be 13 days for Feb,31 days for March and 5 days for Apr.

how can I achieve this?

jjzjx118_2
  • 419
  • 7
  • 23
  • You need to prove it's not your homework. What have you tried so far? – Hao Wu Feb 03 '20 at 09:33
  • Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Tigger Feb 03 '20 at 09:34

2 Answers2

0

You can do this

var d1 = new Date("15/02/2019"); 
var d2 = new Date("04/05/2019"); 
var timeDiff= d2.getTime() - d1.getTime(); 
var daysDiff = timeDiff/ (1000 * 3600 * 24); 

First, get the time difference between them.
Then you can calculcate the difference in days.

Robert Andrei
  • 297
  • 3
  • 15
0

You can use: https://date-fns.org/v1.9.0/docs/differenceInDays

const diff = differenceInDays(
  new Date('2019-2-15'),
  new Date('2020-4-5')
)
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46