1

I want to check if age received from input less than 3 month, lies in range between 3 month and 3 years, or greater than 7 years. So here is my code:

var today = new Date();
var age = (today - new Date('11.11.2010')) / (24 * 60 * 60 * 1000);
var threeMonth = new Date();
var threeYears = new Date();
var sevenYears = new Date();
var threeMonth = new Date(threeMonth.getFullYear(), threeMonth.getMonth() - 3, threeMonth.getDate());
var threeYears = new Date(threeYears.getFullYear() - 3, threeYears.getMonth(), threeYears.getDate());
var sevenYears = new Date(sevenYears.getFullYear() - 7, sevenYears.getMonth(), sevenYears.getDate());
var diff1 = (today - threeMonth) / (24 * 60 * 60 * 1000);         
var diff2 = (today - threeYears) / (24 * 60 * 60 * 1000);         
var diff3 = (today - sevenYears) / (24 * 60 * 60 * 1000);         
if (age < diff1) {                      
console.log('less than 3 month')
} else if (age > diff2 && age < diff3) { 
console.log('greater than 3 month, less than seven years')
} else if (age > diff3) {               
console.log('greater than seven years')
}

I just recently got acquainted with programming, and doubts plagued me about whether I had missed something, and besides, the code is huge and hard to read. Is there any more correct way to solve this problem?

  • 1
    Take a look at [moment](https://momentjs.com/) – ic3b3rg Dec 11 '18 at 21:12
  • `new Date('11.11.2010')` is not a suitable way to generate a Date as the string is not consistent with any of the parsable formats in ECMA-262. Much better to use `new Date(2010,10,11)`. – RobG Dec 11 '18 at 22:10
  • When you add months, you must check the date. 31 Jan + 1 month gives 2 or 3 March (depending on the year) when 28 or 29 February might be required. Similarly for 31 March + 1 month, etc. Also, not all days are 24 hours long where daylight saving is observed, and `new Date()` is not a whole number of days except at exactly midnight so the comparisons will fail around the boundary dates depending on the time that the code is run. – RobG Dec 11 '18 at 22:22

2 Answers2

0

Moment is great for simplifying date calculations. Your logic can also be simplified:

const old = moment('2010-11-11');  // use ISO format
const ageInMonths = moment().diff(old, 'months');

if (ageInMonths < 3) {
  console.log('less than 3 month');
} else if (ageInMonths < 7 * 12) {
  console.log('greater than 3 month, less than seven years');
} else {
  console.log('greater than seven years');
}
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
0

Please see below

    function  inMonths() {

    d1=new Date('1/1/2001');
    d2=new Date('1/1/2002');

    var d1Y = d1.getFullYear();
    var d2Y = d2.getFullYear();
    var d1M = d1.getMonth();
    var d2M = d2.getMonth();

    var diff=(d2M+12*d2Y)-(d1M+12*d1Y);

    if (diff <3)
      alert("Less then three months");
    else if (diff > 3 && diff< 84)
       alert("More then three but less then seven years");  
    else if (diff>84)
       alert("More then seven years");
}
Gaurav
  • 623
  • 5
  • 11
  • This is a vey simplistic approach and counts 31 Jan to 1 Feb is 1 month, which may suit some cases but I don't think that's what the OP wants. – RobG Dec 11 '18 at 23:17