Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
          },
like image 591
Tom Avatar asked Mar 30 '18 20:03

Tom


1 Answers

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

like image 151
Rahul Desai Avatar answered Oct 05 '22 23:10

Rahul Desai