admin管理员组

文章数量:1349240

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?

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?

Share Improve this question edited Aug 2, 2017 at 18:02 user7605325 asked Aug 2, 2017 at 18:00 UtsabUtsab 2092 gold badges5 silver badges12 bronze badges 1
  • 1 I'd start by making sure your syntax is right. What you've posted here is not valid syntax. – Mike Cluck Commented Aug 2, 2017 at 18:04
Add a ment  | 

1 Answer 1

Reset to default 7

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./docs/#/displaying/difference/

本文标签: javascriptCalculate and display difference between two dates using momentjs in ReactjsStack Overflow