Calculate and display difference between two dates using moment.js in React.js?

10,845

You need to call .diff() on a moment object. Try moving the close parenthesis around {props.startDatePicker}

moment({props.startDatePicker}).diff({props.endDatePicker}, 'days');

https://momentjs.com/docs/#/displaying/difference/

Share:
10,845

Related videos on Youtube

Utsab
Author by

Utsab

Updated on June 04, 2022

Comments

  • Utsab
    Utsab almost 2 years

    I am trying to calculate and display number of days between two dates in my React app.

    However, while doing so, I am having issues converting the date from my date picker to a string? Do I even need to convert the date to a string before calculating? Here is what I have.

    var numberOfDays = function({props.startDatePicker},{props.endDatePicker}){
        return { 
            moment({props.startDatePicker}.diff({props.endDatePicker}, 'days'));
        };
    }
    

    What am I doing wrong? Where should I place this code?

    • Mike Cluck
      Mike Cluck over 6 years
      I'd start by making sure your syntax is right. What you've posted here is not valid syntax.
  • lunochkin
    lunochkin over 6 years
    There is no need in adding { } as the whole moment( expression is inside such braces: moment(props.startDatePicker).diff(props.endDatePicker, 'days');
  • bill
    bill over 6 years
    @lunochkin yeah I think you are right. I just left them to avoid confusion so that OP could see the main issue with their syntax.
  • Utsab
    Utsab over 6 years
    Moving the close parenthesis around worked. Thank you!