-2

 function test(val) {
     year = parseInt(val.slice(0,2)); // get year
     month = parseInt(val.slice(2,4)); // get month
     date = val.slice(4,6); // get date

     if (month > 40) { // For people born after 2000, 40 is added to the month.
         year += 2000;
         month -= 40;
     } else {
         year += 1900;
     }

    date = new Date(year, month-1, date, 0, 0);
    date_now = new Date();

   var diff =(date_now.getTime() - date.getTime()) / 1000;
   diff /= (60 * 60 * 24);
   diff = Math.abs(Math.floor(diff/365.25));

   console.log(diff);

}

test("940911") // Should return 25
test("940910") // Should return 26
test("940909") // Should return 26

So If I'm born 1994.09.11 function should returns 25, Because 09.11 is tommorrow, but if I'm born at 1994.09.09, It should returns 26, because 09.09 was yesterday. I do not want to use libreries like moment.js etc.

Sahin Urkin
  • 289
  • 1
  • 8
  • What problem are you having? – T.J. Crowder Sep 10 '20 at 15:28
  • It's probably not the problem (whatever the problem is), but that code is falling prey to what I call [*The Horror of Implicit Globals*](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html). Declare your variables. Not declaring them, in loose mode, makes them globals. – T.J. Crowder Sep 10 '20 at 15:29
  • All of your tests return what they should, based on your comments. – Nathan Champion Sep 10 '20 at 15:29
  • @NathanChampion - That's interesting. I get 26, 26, 26, not 25, 26, 26. What timezone are you in? I'm in British Summer Time (GMT+01:00). – T.J. Crowder Sep 10 '20 at 15:30
  • I also get 26 26 26 – Sahin Urkin Sep 10 '20 at 15:31
  • @T.J.Crowder (GMT-5) Central Daylight Time https://i.imgur.com/5iohNaK.png – Nathan Champion Sep 10 '20 at 15:32
  • Why did you post this question again? Your previous question was closed as duplicate, and so should this one. Please don't repost. Instead edit your original question to make it suitable for reopening, or just use one of the many solutions in the referenced Q&A -- don't only look at the answer that was marked accepted. – trincot Sep 10 '20 at 17:20
  • Why `if (month > 40)`? There are [plenty of existing questions and answers](https://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+dates+in+years%2C+months%2C+days) about getting the difference between two dates. – RobG Sep 10 '20 at 22:16

1 Answers1

1

Here the solution, anyway I suggest you to use moment.

So what about this solution?

Anyway the year? If 01 is 1901 or 2001 ?

function test(d){
        let year = 1900+parseInt(d.slice(0,2)); 
    let month = parseInt(d.slice(2,4)); 
    let day = d.slice(4,6); 
        let yearNow = new Date().getFullYear();
    let monthNow = new Date().getMonth() + 1;
    let dayNow = new Date().getDate();
    if (monthNow === month && dayNow < day || monthNow < month) {
      return yearNow - year - 1;
    } else {
      return yearNow - year;
    }
}
console.log(test("940911"))
console.log(test("940910"))
console.log(test("940909"))
  • It looks that works. I will do some tests and approve your answer if they pass. About years.. I have special condition if year is 01 and and month is over 40 it is 2000, otherwise it is 1900, But this does not apply to the task. – Sahin Urkin Sep 10 '20 at 15:45
  • OK but here is a problem. 940911 is 1994-09-11 in dd/mm/yyyy is 11/09/1994 ? I don't understand the 40 months –  Sep 10 '20 at 15:48