We can! There are a few methods, and quirks, to know about when trying to find the current date using JavaScript.
A great method to know about is new Date() (based on the UNIX epoch, which is the number of milliseconds passed since 1 January 1970 00:00:00 UTC):
new Date() will return a JavaScript Date object with the following format - YYYY-MM-DDThh:mm:SS.sssZ - where the T separates the date from the time and the Z means “zero hour offset” (which is UTC)
JS has built in methods to format the date including:
.getDate(), returns the day of the month
.getFullYear(), returns the year
.getMonth(), returns the month, 0-11 (starting with 0 index)
…among others! Search MDN for more info about getting dates with JS and the methods used to format dates
When we reason it out, there is really no need to declare any of these variables as constants since that makes them not re-usable. With var we at least have the ability to redefine them.
The moment they were defined they were frozen in time. Date.now() changes every millionth of a second. The above is confined to a single instant in time and never changes its timeframe. Alright, one supposes for a one-off session.
I solved the problem Code Challenges: Javascript Fundamentals with if block, however when i try to solve the problem with switch block it gives logical error. can you guide please:
const howOld = (age, year) => {
//calculating current year.
let currentDate = new Date();
let currentYear = currentDate.getFullYear();
//calculating difference in year
const yearDifference = year - currentYear;
const calculatedAge = age + yearDifference;
switch (year) {
case (calculatedAge > age):
return `You will be ${calculatedAge} in the year ${year}`;
break;
case (calculatedAge < 0 ):
return `The year ${year} was ${-calculatedAge} year before you were born`;
break;
default:
return `You were ${calculatedAge} in the year ${year}`;
break;
}
}
console.log(howOld(25, 2024));
It showing following output:
You **were** 30 in the year 2024
But when I do solve the problem with if block it shows:
Hi guys, as far as I can tell my code still works but I can getting a “failed” from codecademy. I am not sure what is wrong. I don’t have any code errors and it prints what I want. Thanks!
// Write your function here:
const howOld = (age, year) => {
let currentDate = new Date();
let currentYear = currentDate.getFullYear();
const calculateBirthYear = currentYear - age;
const calculateAge = year - calculateBirthYear;
const yearDiff = calculateBirthYear - year
if (year > currentYear) {
console.log(You will be ${calculateAge} in the year ${year})
} else if (year < calculateBirthYear) {
console.log(The year ${year} was ${yearDiff} years before you were born)
} else {
console.log(You were ${calculateAge} in the year ${year})
}
}
// Once your function is written, write function calls to test your code!
howOld(28,1995)
Is it not possible to get current year based on Date without declaring the variable first?
Similar to what we do with Math.floor(math.random()); like so:
const currentYear = getFullYear(Date());
or
const currentYear = getFullYear.Date();
I’ve tried both with no luck but I’m not sure I understand why. What is the difference between 1 and 2 above?
If the year passed in was before the person was born, the function should return: ‘The year [year passed in] was [calculated number of years prior to their birth] years before you were born’
Can you help me?
// Write your function here:
function howOld(age,year){
let currentDate = new Date();
let theCurrentYear = currentDate.getFullYear();
const yearDifference = year - theCurrentYear;
const newAge = age + yearDifference;
const ageDifference = age-newAge;
if(newAge < 0){
return `The year ${year} was ${ageDifference} years before you were born`
}else if(newAge > age){
return `You will be ${newAge} in the year ${year}`;
}else if(newAge < age){
return `You were ${newAge} in the year ${year}`;
}else{
return `Input future or past year `
}
}
// Once your function is written, write function calls to test your code!
console.log(howOld(24,1990));
function howOld(age, year) {
let currentDate = new Date();
let theCurrentYear = currentDate.getFullYear();
const yearDifference = year - theCurrentYear;
const newAge = age + yearDifference;
if (newAge < 0) {
return `The year ${year} was ${Math.abs(newAge)} years before you were born`;
} else if (newAge > age) {
return `You will be ${newAge} in the year ${year}`;
} else if (newAge < age) {
return `You were ${newAge} in the year ${year}`;
} else {
return `Input future or past year `;
}
}
console.log(howOld(32, 2030));
your const ageDifference = age-newAge; is incorrectly calculated if the person was born before that year. Your const newAge = age + yearDifference; is perfect to use because it will either be a positive or a negative. So when it is a negative you can just use the Math.abs() method to convert it to a positive for the first condition of your if statement
I believe that your problem is that you are using a the console.log function inside your howOld function when what you should be doing is using return to get the value back. When you initialized the function you did so by calling it with the line “howOld(28, 1995)” this means it is expecting a value to be returned but console.log is not defined so you get the failed message. Remove the console.log and replace it with a simple return, that will fix the problem.
Hi there,
I made this solution and it’s works. but the “next” bottom still not allow me to the next challange.
Would you like to review this one:
const howOld = (age, year) => {
const yearDifference = year - 2020;
const newAge = age + yearDifference;
const yearBirth = 2020-age;
const beforeBirth = yearBirth - year
if(year>2020) {
return You will be ${newAge} in the year ${year}
}else if (year<1990) {
return The year ${year} was ${beforeBirth} years before you were born
} else if (year<2020) {
return You were ${newAge} in the year ${year}
}
}
I believe the exercise expects you to use Date object to calculate the expected values. You shouldn’t be using any literals within your code.
@mtf provided a solution to getting the current year:
Additionally, the problem can be solved with less than four variables within the function. Although, I’m not entirely certain that is a requirement in order to solve without errors. There’s more than one way to skin a cat