简体   繁体   English

使用React.js中的moment.js计算并显示两个日期之间的差异?

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

I am trying to calculate and display number of days between two dates in my React app. 我试图在我的React应用程序中计算并显示两个日期之间的天数。

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? 我应该在哪里放置此代码?

You need to call .diff() on a moment object. 您需要在矩对象上调用.diff() Try moving the close parenthesis around {props.startDatePicker} 尝试围绕{props.startDatePicker}移动{props.startDatePicker}括号

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM