2

I am trying to check if the date i.e. (07/02/2018 ) is more than year from current date and if the date i.e. (07/02/2018 ) is less than year from current date using JavaScript tried with following code

var x = new Date('JUN-2016');
var y = new Date('MAY-2016');
console.log(+x < +y);
Ivan Beldad
  • 2,285
  • 2
  • 21
  • 33
user3396954
  • 55
  • 1
  • 7

6 Answers6

1

Your question is not very clear to me but this may help you:

var now = new Date();
var then = new Date('07/02/2018');
var diffInDays = Math.round((then-now) / (1000*60*60*24));

console.log('then is', diffInDays, 'days more than now.');

Note: If diffInDays is a positive number then the then is more than now, otherwise it's less.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

You can simply add 1000*60*60*24*365 milliseconds to your first date and compare it to your second date :

var x = new Date('APR-2015');
var y = new Date('MAY-2016');

console.log(+x + 1000*60*60*24*365 < +y);

var x2 = new Date('SEP-2015');

console.log(+x2 + 1000*60*60*24*365 < +y);
Zenoo
  • 12,670
  • 4
  • 45
  • 69
0

You can get the year with the getFullYear()method (Date doc).

let x = new Date();
let y = new Date();

x_year = x.getFullYear();
y_year = y.getFullYear();

// The you just have to compare the year
  • 1
    So, for you January 2015 is one year away from December 2014 ? – Zenoo Feb 07 '18 at 13:30
  • 1
    Oh okay I didn't understand well, you can do that and also compare the month, it will give you the exact difference. But Zenoo's answer looks better. –  Feb 07 '18 at 13:37
0

One way to do it, although I'd be surprised if there wasn't a better way:

var x = new Date();
x = x.setFullYear(x.getFullYear() + 1);
x = new Date(x);
var y = (new Date('2018-04-08'));
var z = (new Date('2019-04-08'));

if (y < x) {
    console.log('Y is less than a year from now');
} else {
    console.log('Y is more or exactly a year from now');
}

if (z < x) {
    console.log('Z is less than a year from now');
} else {
    console.log('Z is more or exactly a year from now');
}
Asgeirr
  • 82
  • 10
  • this is similar but can i check if the two dates y and z has more than year and less than year – user3396954 Feb 08 '18 at 06:20
  • If I understand you correctly, yes. Just do lines 2 and 3 to whatever date you want to add a year to, then compare that to the other date. – Asgeirr Feb 08 '18 at 09:46
0

Your question is not clear this is based on my assumption. I am assuming that you need to check if the given date is one year ahead of the current date.

var x = new Date('1, 1, 2018');
var today = new Date();
var time = Math.abs(today.getTime() - x.getTime());
var days = Math.ceil(time / (1000 * 3600 * 24));
alert("There is a difference of " + days + " days between today and the given date");
Navi
  • 40
  • 2
  • 12
0

I am assuming that you want to know if some arbitrary date is less or more than a year from current date.

We can get a the value for a year this way:

var a_year = Math.abs(new Date('01-01-2018'.replace(/-/g,'/')) - new Date('01-01-2017'.replace(/-/g,'/')));

Then we set two date tests:

var test1 = '01-01-2018';

var test2 = '01-01-2015';

Calculate the diffs:

var diff1 = Math.abs(new Date() - new Date(test1.replace(/-/g,'/'))); var diff2 = Math.abs(new Date() - new Date(test2.replace(/-/g,'/')));

Then you can log the results for testing:

console.log(diff1 > a_year) console.log(diff1 < a_year) console.log(diff2 > a_year) console.log(diff2 < a_year)

Credits to David Hedlund's answer on How to subtract date/time in javascript?

italo.portinho
  • 121
  • 1
  • 5