-4

I'm very new to Javascript and can't figure out the following:

1) Ask user to submit their full birthdate (not just the year) and then calculate if their birthday has already happened this year, is today or will be coming up this year.

2) Get a customized message to display depending on these three possible outcomes (eg/ "you already had your birthday this year", "today's your birthday", "your birthday is coming up")

I know it must be simple, but I've googled for a couple days now and can't figure it out.

What I have so far:

function getAge(birth){
  var today = new Date();
  var nowYear = today.getFullYear();
  var nowMonth = today.getMonth();
  var nowDay = today.getDate();
  
  var birthYear = birth.getFullYear();
  var birthMonth = birth.getMonth();
  var birthDay = birth.getDate();
  
  var age = nowYear - birthYear;
  var age_month = nowMonth - birthMonth;
  var age_day = nowDay - nowDay - birthDay;
  
  if (age_month < nowMonth || age_date < nowday) {
    age = parseInt(age) -1;
  }
  alert("your birthday just happened");
}

  
andrepaulo
  • 816
  • 11
  • 24
  • 1
    Do you have any code to show us? or is it a spec? kk – andrepaulo Feb 02 '17 at 14:38
  • ok boss ahah... – Fanyo SILIADIN Feb 02 '17 at 14:39
  • Put the relevant parts of your code *text* in your question. – Matt Burland Feb 02 '17 at 14:40
  • 1
    divide and conquer. search for how to `prompt` people with a question. search for how to compare dates. search for if/else examples. etc. – Seth Flowers Feb 02 '17 at 14:41
  • I can't figure out how to paste the code in my message but there's a link "my javascript" that opens a window to it. I'll add what I have so far too. – vaguelyexotic Feb 02 '17 at 14:46
  • You can use http://jsfiddle.net/ to put your code – Rohan210 Feb 02 '17 at 14:53
  • for the code to show up as a code block, it needs to be indented 4 spaces and a blank line or two from regular text – Bindrid Feb 02 '17 at 14:55
  • Thanks, Bindrid. Posted the code but doubt I'll get any help (I know I'm pretty useless here and my code probably isn't very close to the answer) I'll keep looking for the answer. I'm struggling with knowing how everything works together and I'm probably missing the fundamental logic...just don't understand how to get everything to work together. Thanks anyways to those who tried to help. – vaguelyexotic Feb 02 '17 at 15:07
  • Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Heretic Monkey Feb 02 '17 at 15:58

1 Answers1

2

VaguelyExotic I think this can help you!

It prompts for the user to enter the date he/she was born. compare the month + day with the current month + day, and accordingly shows the message.

EDIT - This is a simpler version to help you understand the steps that need to be made to solve your problem, it supposes the user is always going to enter the date correctly and has no exception treatment. So feel free to improve this. It's just for you to start your own version.

function getAge(){
  var today = new Date();
  var nowYear = today.getFullYear();
  var nowMonth = today.getMonth();
  var nowDay = today.getDate();
  
  var birth = prompt("When were you born?", "YYYY-MM-DD");
  var birth = new  Date(parseInt(birth.substring(0,4)),parseInt(birth.substring(5,7))-1,parseInt(birth.substring(8,10)));
  
  var birthYear = birth.getFullYear();
  var birthMonth = birth.getMonth();
  var birthDay = birth.getDate();
  
  var compBirth = birthMonth.toString() + birthDay.toString();
  var compToday = nowMonth.toString() + nowDay.toString();
  
  
  if( compBirth == compToday) {
    alert('Today is your birthday!');  
  } else if ( compBirth > compToday){
    alert('Your birthday is comming!');  
  } else {
    alert('Happy b-lated day!');  
  }
    
}
getAge();
andrepaulo
  • 816
  • 11
  • 24
  • Thank you so much! The code works great and I really like that it's clean and I can understand most of it - I'll study it to understand how it works but already it's making more sense. Very much appreciated. – vaguelyexotic Feb 02 '17 at 16:14
  • So please, accept this as your answer ;), I'd appreciate. I feel glad I could help – andrepaulo Feb 02 '17 at 16:21
  • Not sure if this is still relevant.. But it won't work if today is January and birthday was in December.. It'd say "Your birthday is comming!" – piyushkantm Apr 18 '19 at 21:39
  • 1
    The correct way to check is find diff in days from today to dd-mm-lastYear and diff in days from dd-mm-nextYear from today.. and comparing which is greater.. – piyushkantm Apr 18 '19 at 21:41