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/
Related videos on Youtube
Author by
Utsab
Updated on June 04, 2022Comments
-
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 over 6 yearsI'd start by making sure your syntax is right. What you've posted here is not valid syntax.
-
-
lunochkin over 6 yearsThere is no need in adding
{
}
as the wholemoment(
expression is inside such braces:moment(props.startDatePicker).diff(props.endDatePicker, 'days');
-
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 over 6 yearsMoving the close parenthesis around worked. Thank you!