How to use getDate() and setDate()

Hi guys, I’m stuck with this exercice:

// write a function called tellAge that tells you how old you are based on the date of birth passed.
// this function will take 3 arguments month, day and year
// You should use the Date object to set the value of today.
// if the birth date is less than one year from the current date it should return : “You are NUMBER_DAYS old”
// if its more than one year it should return you are NUMBER_YEARS old"

Can’t figure out where to start from and how to use Date to set the value of today.

Can anyone help me, pls?

Welcome, elisa.

The best thing to do is to research in the official Mozilla Docs: Date.prototype.getDay() - JavaScript | MDN (

Hope that helps

Hi @elisa.colombano, welcome to the forum.

Besides the challenge requirements, we should get an indicator also on your familiarity to the subject as well as what you actually have tried so far to achieve your goal.

Without those is pretty hard to help you without spoiling the solution.

For starters, have you check the available methods for the Date object?

Best of luck and happy coding :sparkles:

1 Like

Thank you. It helped a lot, but I’m still struggling with the exercice. I’ll send my code. Thanks

Thanks @Marmiz . I’ve started a couple of weeks ago with javascript. I’ve tried to build the solution but is not working.

This is my code:

function tellAge (day, month, year) { // Define a function called ‘tellAge’ which takes an 3 arguments:
// a day, a month, and a year.

let today = new Date();                      // Inside the function create 2 Date objects -- one for today
let birthd = new Date (day, month, year);    // and another for the date of birth provided in the arguments.

let timeDiff = today - birthd               // Create a new variable called 'timeDiff' and as a value 
                                            // assign results of subtracting the date of birth from today

if (timeDiff < 1) {
    return `You can't be born in the future!`;

let days = Math.floor(timeDiff / 86400000)           // Create a new variable called 'days'
                                           // Convert 'timeDiff' which is in ms to days and round it with Math.floor.

if (days > 365) {
    return `You are ${days / 365} years old`;
} else {
      return `you are ${days} days old`;


For the most part, your project works. Close, but not quite.

There are 3 things I can recommend at this point.

First, your usage Date on the line let birthd = new Date... is wrong. I would recommend reading into these docs on how to use Date methods, and look closely at their examples. Date is complicated and often confusing. Personally I use Dev Docs a lot, too, and it makes looking things up much easier.

Secondly, and this isn’t primarily your fault, it’s just how Date works, time zones ruin your math. At some point, either when you’re defining today or birthd, a time zone is being attached automatically. It’s possibly even happening at both times.

For instance, you’re taking today’s date, let’s assume you just want 12/21/2020.

If I run new Date() at this very moment, what I get is this:
Mon Dec 21 2020 18:09:35 GMT-0600 (Central Standard Time)

I don’t get just the day, the hours and so forth are also measured.

If I use new Date to request the specific date of Dec 30th, 2010, I get:
Thu Dec 30 2010 00:00:00 GMT-0600 (Central Standard Time)

Doing the math you want to do may be easier if you convert your date objects to Unix date numbers with date.valueOf() immediately, and then doing your math and diving seconds into years like you did. Also double check whatever date objects you are initially creating are in the same time zone before you start your calculations. Also, months in Date objects are zero-indexed, so January is 00, not 01.

Third, when I did get the “correct” answer, it was a floating number instead of an integer with many decimal points. Being xx.xxxxxxxxxxxxx years old isn’t usually an acceptable answer for most people.

Fourth, as a bonus, your code is offering me no restrictions for input. I could put 999999 in each of the 3 arguments and it would break your code.

In conclusion, Date can be a very powerful tool but it’s also very confusing and often inconsistent.