Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment.js - isBetween() method doesn't work consistently

I have a moment.js code that works well:

var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(moment(), moment().add(30, 'days'));
// returns true, that's great!

But when I start refactoring to make it more readable it fails to work:

var today = moment();
var startDate = '2015-05-06T19:00:00+0300'; 
moment(startDate).isBetween(today, today.add(30, 'days'));
// returns false.. but why?
like image 981
Kosmetika Avatar asked May 02 '15 09:05

Kosmetika


People also ask

Is MomentJS still used?

MomentJS is a widely used time and date formatting and calculation library.

Is JavaScript moment immutable?

Mutability 1.0.The moment object in Moment. js is mutable. This means that operations like add, subtract, or set change the original moment object.

How do I get the current time in MomentJS?

To get the current date and time, just call javascript moment() with no parameters like so: var now = moment();


1 Answers

var today = moment();
var startDate = '2015-05-06T19:00:00+0300'; 
moment(startDate).isBetween(today, moment(today).add(30, 'days')); 

You are passing a reference which you have edited by adding 30 days.

like image 58
Niels Avatar answered Oct 19 '22 23:10

Niels