Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Harry Beckwith
13,452 PointsJavaScript date calculation
Hi,
I was wondering if anyone could solve this?!?!
I am looking to create code in javaScript that does the following:
- A button that can be clicked to bring up a prompt box.
- The prompt box ask the user for their date of birth.
- A alert box is displayed to show how old the user is.
I have tried to solve this but i have been unable to !
Help appreciated !
Thank you =)
Harry
2 Answers
Himanshu Chopra
3,055 PointsHello Harry! I could come up with this small program which ask user to enter his/her birth year and then prints his age.
var date = new Date(); // It creats th new date object from the specified date and time
var currentYear = date.getFullYear(); // Gets the Current Year
var birthYear = prompt('Hello Friend! In which year you born?'); // Prompt user to enter his/her birth year
var userAge = currentYear - birthYear; // Substract the current year and the year in which user was born
document.write('<h3> You are ' + userAge + ' years old </h3>') // Prints out the user age
Well bro even I am new to JavaScript and this might look a simple program to you but do you thik it's fine? can we do more with it?
Thanks mate:)
Himanshu Chopra
3,055 PointsYou are most welcome Harry. I am glad that I helped some how :)
Harry Beckwith
13,452 PointsHarry Beckwith
13,452 PointsHello Himanshu, nice work! just what i was looking for!
Thank you