-2

I'm trying to take someone's birthday and find the day that he/she turns 65. I have the following code but the calculation seems off. For example, if I use the birthday 11/15/1957 I should get 11/15/2022 as the 65th birthday. However, I'm getting 8/9/1982 (which is incorrect) instead.

Am I missing something?

function convertDate(birthday) {

  var usethisdate = new Date(birthday);

  //let's start by finding the person's 65th birthday

  var birthdaysixtyfive = new Date(usethisdate.setMonth(usethisdate.getMonth() + 780));
  console.log("65th birthday: ", birthdaysixtyfive.toDateString());
}

convertDate('11/15/1957');
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Do you want to get the day like sunday, monday etc..?? – p4avinash Apr 07 '22 at 05:22
  • 1
    This is working fine for me. https://jsfiddle.net/uxw07mbf/ – Nitheesh Apr 07 '22 at 05:24
  • Exactly what value are you using for `birthday`? The `Date` constructor is picky about what you pass to it – Phil Apr 07 '22 at 05:26
  • Converting your code to a runnable snippet shows it returns the expected result. Voting to close as not reproducible. – RobG Apr 07 '22 at 10:59
  • I'm using I'm creating a date object, assigning the following date and trying to pass it through the function. ``` const givenDate = new Date(11,15,1957); convertDate(givenDate); ``` I'm wanting to get a result "65th birthday is: 11/15/2022" – wesb2013 Apr 07 '22 at 15:04
  • I ran the code snippet on this website and it looks like it worked, I'm not getting the results to translate into google Apps Scripts. Is there something I should be looking at? – wesb2013 Apr 07 '22 at 15:56

3 Answers3

0

Just add 65 to the provided year. And you'll get the result. Below code should do the work.

let birthDay = new Date('11/15/1957')

function getDay(birthDay){
  let birthYear = birthDay.getFullYear() + 65
  let birthArr = birthDay.toDateString().split(' ')
  let calculatedDay = new Date(`${birthYear}-${birthArr[1]}-${birthArr[2]}`).toDateString()
  return calculatedDay
}

console.log(getDay(birthDay))
p4avinash
  • 548
  • 2
  • 4
  • 15
  • 1
    I get "Invalid Date". See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 07 '22 at 10:52
-1

Your code is ok. I think you want like this format :

function convertDate(birthday) {

var usethisdate = new Date(birthday);

//let's start by finding the person's 65th birthday

var birthdaysixtyfive = new Date(usethisdate.setMonth(usethisdate.getMonth()+780));

    birthdaysixtyfive

    bd65 = (birthdaysixtyfive.getMonth() + 1)+'/'+birthdaysixtyfive.getDate()+'/'+birthdaysixtyfive.getFullYear();
    
    console.log("65th birthday: ",bd65);
}


convertDate('11/15/1957');

Thanks.

Pran Paul
  • 68
  • 6
-2

I would recommend using the 'momentjs' library. It has a function for difference.

const moment = require('moment')
function convertDate(birthday) {

    var usethisdate = new Date(birthday);
    
    //let's start by finding the person's 65th birthday
    
    var age  = moment().diff(usethisdate, 'years');
    console.log("Age: ",age);
    var currentDate = moment();
    var year = moment(currentDate).add(age, 'Y').format('DD-MM-YYYY'); //or replace age with 65
    console.log("Xth birthday: ",year);
    }

    convertDate(11/15/1957)
  • 2
    Moment.js themselves recommend not using that library for new code. See https://momentjs.com/docs/#/-project-status/ – jnpdx Apr 07 '22 at 05:40