question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught TypeError: date2.diff is not a function (problem 1)

See original GitHub issue

The bug I have the function used in litepicker like this: nights.value = date2.diff(date1, ‘day’); This worked in version 1.0.18 I updated litepicker to the newest 1.4.6 and this suddenly stopped working.

To Reproduce

const picker = new Litepicker({
singleMode: false,
onSelect: function (date1, date2) {
nights.value = date2.diff(date1, 'day');
}
});

Expected behavior This should give back the days in numbers.

jsfiddle.net / codepen.io / etc https://codepen.io/kevinmu/pen/PoZNXzB

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinmu17commented, Jun 16, 2020

I don’t see .diff() as a function of a date object in JavaScript.

I’d have to look, but isn’t that a component of moment.js? I believe that Moment.js was a dependency of the deprecated lightpick picker (that litepicker replaced), but not this picker.

I thought so yes, but there is a piece of code within this current version of litepicker using the same code 😃 https://github.com/wakirin/Litepicker/blob/13b64c52cbb999a282f9863a92736a1f76915b69/src/litepicker.ts#L636

This is why I thought something was broken. But your solution is working perfectly, thanks!

1reaction
scott-fisher-iiicommented, Jun 12, 2020

I don’t see .diff() as a function of a date object in JavaScript.

I’d have to look, but isn’t that a component of moment.js? I believe that Moment.js was a dependency of the deprecated lightpick picker (that litepicker replaced), but not this picker.

In any case, getting the days difference between two dates can be achieved by:

// Convert milliseconds difference to days
var dayCount = Math.ceil(Math.abs(date2 - date1) / 86400000);
// 86400000 = (1000 * 60 * 60 * 24);
console.log("Difference: " + dayCount + " days");
Read more comments on GitHub >

github_iconTop Results From Across the Web

Diff is not a function in Moment.js - javascript - Stack Overflow
The diff function is only available on moment objects. Try this instead: var now = moment(new Date()), end = moment(fd), days = end.diff(now ......
Read more >
Problem with the diff() function · Issue #723 · moment ... - GitHub
I try for example this code : moment('2012-02-03').diff(moment('2012-02-06'), 'd') it gives me 0, but the answer should be 3... am I not ......
Read more >
Moment.js moment().diff() Function - GeeksforGeeks
diff () function is used to get the difference in milliseconds of given dates which are passed as parameter to this function. Syntax:...
Read more >
[Solved]-.diff is not a function in Moment.js-moment.js
Via the docs, moment().format() returns a String, so your x and y variables are both Strings. If you need to both do calculations...
Read more >
TypeError: getFullYear() is not a Function in JavaScript
To solve the "getFullYear is not a function" error, make sure to only call the getFullYear() method on a valid Date object, e.g....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found