简体   繁体   中英

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. Why does minutesDifference return NaN ? My goal is to get number of minutes between the given dates. Here is the link to the Date-FNS doc.

 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() .

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.

Please refer the docs: Date - JavaScript | MDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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