0

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?

emarel
  • 371
  • 7
  • 30
  • Create 2 date objects for same year, then you can calculate the difference – Satpal May 04 '16 at 22:38
  • 3
    how many days are between feb28 and mar1? – xaxxon May 04 '16 at 22:44
  • 2
    @Satpal that would work because what if you had December 28, 2015 and January 10, 1992 – emarel May 04 '16 at 22:46
  • It depends what you want, if negative number is acceptable then it will work fine otherwise check if 2nd date is smaller then add 1 year then calculate difference – Satpal May 04 '16 at 23:10
  • Why not `var diffDays = a.diff(b, 'days') % 365`? Of course if it may be out by 1 day depending on leap years. – RobG May 04 '16 at 23:41
  • Unfortunately that 1 day is why I cannot mod it – emarel May 04 '16 at 23:44
  • 1
    Iunno, seems to me like if you need it to count smoothly from Dec 25th, 2016 to Jan 5th, 2017, then you are not "ignoring the year"; In this edge case I would somehow just use the actual years on both the dates and do a regular diff. Perhaps do a conditional where I check the difference and if it's less than 365 days, then revert to a diff using the original years. – Montagist May 04 '16 at 23:50
  • 1
    So the years **do** matter, as the update to your question points out. You'll need to provide an algorithm for handling that, or wait for people to guess one that suits. E.g. if leap year… if start year is before end year but start month is after end month… and so on. – RobG May 04 '16 at 23:53
  • 1
    It seems to me you want an answer like the ones to [*How to get difference between 2 Dates in Years, Months and days using moment.js*](http://stackoverflow.com/questions/26063882/how-to-get-difference-between-2-dates-in-years-months-and-days-using-moment-js), then convert the months to days and ignore the years (though you still have to work out if February is 28 or 29 days). There are also lots of [*non–moment.js answers*](http://stackoverflow.com/search?q=%5Bjavascript%5D+difference+in+years+months+and+days). – RobG May 04 '16 at 23:57

3 Answers3

1

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' ) );
}
Montagist
  • 394
  • 2
  • 8
  • would this cover the scenario of one object being in December and the other being in January of another year? – emarel May 04 '16 at 22:48
  • yes. the diff method can return a negative value, so use Math.abs on the return value of the diff – Montagist May 04 '16 at 22:52
  • I updated above to include a scenario that breaks this. Any help would be appreciated – emarel May 04 '16 at 23:43
1

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

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • There aren't always 365 days in a year. And when to account for leap year, in start year? End year? Only when dates either side of 28/29 Feb? (those are more questions for the OP…). – RobG May 05 '16 at 01:22
  • As I said in the answer, I think it's clear that this aproach (removing the year), it's error prone. But if he asked it in the first place he should have a reason for this. So, I guess this 1 day difference, won't make much difference to him. – Nelson Teixeira May 05 '16 at 02:02
0

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
fergardi
  • 35
  • 1
  • 2
  • 6