简体   繁体   English

使用Date-FNS以分钟为单位获取日期之间的差异-NaN错误

[英]Get the difference between to dates in minutes with Date-FNS - NaN Error

I'm using the Date-FNS library to get the difference between to dates in minutes. 我正在使用Date-FNS库来获取分钟之间的日期之间的差异。 Why does minutesDifference return NaN ? 为什么minutesDifference返回NaN My goal is to get number of minutes between the given dates. 我的目标是获取给定日期之间的分钟数。 Here is the link to the Date-FNS doc. 这是指向Date-FNS文档的链接。

 getDateTime: function () {
            var now = new Date()
            var year = now.getFullYear()
            var month = now.getMonth()
            var day = now.getDate()
            var hour = now.getHours()
            var minute = now.getMinutes()

            var dateTime = year + ', ' + month + ', ' + day + ', ' + hour + ', ' + minute + ', 0'

            var minutesDifference = differenceInMinutes(new Date(2018, 2, 30, 22, 55, 0), new Date(dateTime))
            return minutesDifference
          },

Your value of dateTime is a string which is incorrect, you are passing 1 single param and not 6 different params to Date() . 您的dateTime值是一个不正确的字符串,您向Date()传递了1个单一参数,而不是6个不同的参数。

Just do 做就是了

differenceInMinutes(new Date(2018, 2, 30, 22, 55, 0), new Date())

new Date will take the current date/timestamp by default so you dont need to pass any params to it. 默认情况下, new Date将采用当前日期/时间戳,因此您无需将任何参数传递给它。

Please refer the docs: Date - JavaScript | 请参考文档: Date -JavaScript | MDN MDN

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

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