emarel
emarel

Reputation: 401

Moment JS: Difference of days ignoring the year

I am familiar on how to find the difference of two dates given two moment object a and b:

let diffDays = a.diff(b, 'days');

What I am having a problem with is ignoring the year. I only want moment a and b to find the difference of days disregarding what year it is.

Example:

a = June 1, 2015
b = June 5, 1992 //desired outcome 4. Not 4 + 4*365

let diffDays = a.diff(b, 'days')

How would I go about this?

UPDATE

The scenario that breaks most answers is below:

let c = moment( "January 5, 2015" );
let d = moment( "December 25" ).year( c.year() );
console.log(Math.abs( c.diff( d, 'days' ) ));

Output: 354 Desired: 11

Setting the year to the same looks like it causes issues. Any solution?

Upvotes: 0

Views: 1829

Answers (3)

fergardi
fergardi

Reputation: 45

Let's say I want to know if today's date is between Halloween period, no matter the year:

const now = moment() // 31/10/2017
const begin = moment('15/10/____', 'DD/MM/____') // begin
const end = moment('15/11/____', 'DD/MM/____') // end
now.isBetween(begin, end, 'days', '[]') // true

Upvotes: 0

Nelson Teixeira
Nelson Teixeira

Reputation: 6572

You just have to set the year of both dates to the same year. Here is an example:

var a = moment("June 1, 2015");
var b = moment("June 5, 1992");

a.year(2000);
b.year(2000);

var diffDays = a.diff(b, 'days')
console.log(diffDays);

I choose 2000 as a random year number, pick anything you want.

you can also set b year to a year like this:

b.year(a.year());

and use Math.abs if you want an absolute diffence rather than a negative or positive number difference:

var diffDays = Math.abs(a.diff(b, 'days'));

Update

For the 2nd example. That's relatively easy:

var c = moment( "January 5, 2015" );
var d = moment( "December 25, 2014" )
if (c > d)
    c.year( d.year() + 1 );
 else
    c.year( d.year() );
 console.log(Math.abs( c.diff( d, 'days' ) ));

but you're seeing where this takes... how do you know if you want one date from a year and the other from another ? You have to establish some kind of rule.

For example if you don't need exact results, maybe you can establish that if a diff if greater than 1/2 an year you would consider that it's from the next year. Like this:

var c = moment( "January 5, 2015" );
var d = moment( "December 25, 2014" )
c.year( d.year() );
var dif = Math.abs( c.diff( d, 'days' ) );
if (dif > (365/2))  c.year( d.year() + 1 );
dif = Math.abs( c.diff( d, 'days' ) );

console.log(dif);

I think this is enough to get you started. :)

Upvotes: 1

Montagist
Montagist

Reputation: 401

Manually instantiate another momentjs instance, merely reusing the month and day from the first date but the year from the second. Then call your diff method.

var a = moment( "June 1, 2015" ),
    b = moment( "June 5, 1992" ),
    origDiff = Math.abs( a.diff( b, 'days' ) ),
    finalDiff;

if ( origDiff < 60 ) {

    finalDiff = origDiff;

} else {

    b.year( a.year() );
    finalDiff = Math.abs( a.diff( b, 'days' ) );
}

Upvotes: 1

Related Questions