'Calculate the difference between two dates in javascript using react

I'm trying to get the difference between two date in react whith two datepicker and two timepicker ( a react component ). The date is for a booking and I want to get the duration by substract the "end date" with "start date", but when I subtract two date whith more than 24 hours I have a -152 or a value that not corresponding to the real duration.

The code

periodReserve = (e, idPark, title ) => {
let Start_Day = this.state.startDate && this.state.startDate.format(Formatdate);
let Start_Hour = this.state.time && this.state.time.format(formatHour);
let End_Day = this.state.endDate && this.state.endDate.format(Formatdate);
let End_Hour = this.state.timeEnd && this.state.timeEnd.format(formatHour);
let diff = this.state.timeEnd - this.state.startDate;
console.log(diff);}

The error

The code for the first DatePicker and the TimePicker ( for the start )

<DatePicker
  id="calendar"
  className="TestIcon"
  dateFormat='YYYY-MM-DD'
  placeholderText="Date de fin"
  selected={this.state.startDate}
  onChange={this.handleChangeStart}/>

<TimePicker showSecond={false}
     placeholder={time}
     onChange={this.onChangeStart}> </TimePicker>

The code for the second dataPicker and TimePicker

<DatePicker
    id="calendar"
    className="TestIcon"
    dateFormat='YYYY-MM-DD'
    selected={this.state.endDate}
    onChange={this.handleChangeEnd}/>

 <TimePicker showSecond={false}
  placeholder={timeEnd}
  onChange={this.onChangeEnd}> </TimePicker>


Solution 1:[1]

  const dateConverter = (startDate, timeEnd) => {
    const newStartDate= new Date(startDate);
    const newEndDate=new Date(timeEnd);
    const one_day = 1000*60*60*24;
    let result
    result = Math.ceil((newEndDate.getTime()-newStartDate.getTime())/(one_day))
    console.log('date Converter result', result)
    if (result < 0 ) {return 0}
    return result
  }

Result in Days

Solution 2:[2]

it is small libaray

click for reference

npm i moment

  const dateConverter = (startDate, timeEnd) => {
     const newStartDate= new Date(startDate);
     const newEndDate=new Date(timeEnd);
     let result=moment(newStartDate).diff(newEndDate,'days')
     return result   }

Solution 3:[3]

Quick remark to the snippet

If you want absolute values like 250 days. You should use this:

diffDuration.asDays()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 olaleye olabanjo
Solution 2
Solution 3 koo