James111
James111

Reputation: 15903

cannot find amount of days until next birthday for the user input

Below will get the amount of days until the persons birthday, If the birthday has already been it will then give you the previous days of when your birthday was!

I'm Still wondering weather it's possible to calculate the amount of days until the next birthday, This would require going past december and keep on counting from january. I can't seem to get it right.

 //Getting birthday and month form form
 var birthdayMonth = document.getElementById('selMonth').value;
 var birthdayDay = document.getElementById('selDay').value;

//Parsing Birthday and month
birthdayMonth = parseInt(birthdayMonth);
birthdayDay = parseInt(birthdayDay);

    //setting date object
    today = new Date( );          // set today's date
    birthday = new Date( );      // set up the Birthday object
    birthday.setMonth(birthdayMonth);      // set birthday month to December
    birthday.setDate(birthdayDay);         // set birthday date to the 15th

if (today < birthday)
 {  //this gets days until next birthday - 
     diff = Math.abs(birthday.getTime( ) - today.getTime( ));
     diff = Math.floor(diff / (1000 * 60 * 60 * 24));
     alert('There are ' + diff + ' days until your birthday ');
  }
else
{ //This gets days since last birthday - 
    diff = Math.abs(today.getTime( ) - birthday.getTime( ));
    diff = Math.floor(diff / (1000*60*60*24));
    alert('It was ' + diff + ' days since your last birthday');

}

Upvotes: 1

Views: 1202

Answers (4)

James111
James111

Reputation: 15903

Here I have completed the task I was trying to do... This will check how many days it was since there last birthday. This will check how many days until there next birthday. It will also check if todays the users birthday.

Thanks everyone for helping me get to this conclusion. With your help I was able to complete it! I hope this helps anyone trying to do something similar.

var output = '';
        var birthdayMonth = document.getElementById('selMonth').value;
        var birthdayDay = document.getElementById('selDay').value;
        
        birthdayMonth = parseInt(birthdayMonth);
        birthdayDay = parseInt(birthdayDay);
        
        //Date Objects
            today = new Date( );          // set today's date
            birthday = new Date( );      // Birthday object setup e.g(birthday.getTime());
        
            birthday.setMonth(birthdayMonth);      // set birthday month (userInput)
            birthday.setDate(birthdayDay);         // set birthday date (userInput)
        
        //Check if todays the users Birthday
        if(birthday.valueOf() == today.valueOf()){
            sweetAlert("Happy birthday!");  
        }
        
        //Until Next Birthday
        if (today > birthday)
        {
        // If the birthday is passed it will calculate how long until it comes next, even if it's next year.
            birthday.setYear(today.getFullYear() + 1); // Set Year = current year + 1
            diff = Math.abs(birthday.getTime( ) - today.getTime( ));
            diff = Math.floor(diff / (1000 * 60 * 60 * 24)); 
            //alert('There are ' + diff + ' days until your birthday'/*Add this for actual date -> birthday*/); alerting how many days
            output += 'There are ' + diff + ' days until birthday'/*Add this for actual date -> birthday <- */;
        }
        
        //Days since last birthday! e.g(may 20 till today(oct 29))
            birthday.setYear(today.getFullYear()); // Set year is normal. (no +1)
            PRdiff = Math.abs(today.getTime( ) - birthday.getTime( ));
            PRdiff = Math.floor(PRdiff / (1000*60*60*24));
            //alert('It was ' + PRdiff + ' days since your last birthday');  Alerting how many days
            output += '<br />It was ' + PRdiff + ' since your last birthday';
            
            //Output it to the page
            document.getElementById('birthdayOutput').innerHTML = output;

Upvotes: 1

Sam
Sam

Reputation: 2917

Here's the answer if you need to calculate dates to B'day irrespective of the year

var birthdayMonth = document.getElementById('selMonth').value;
var birthdayDay = document.getElementById('selDay').value;

birthdayMonth = parseInt(birthdayMonth);
birthdayDay = parseInt(birthdayDay);

    today = new Date( );          // set today's date
    birthday = new Date( );      // set up the Birthday objectbirthday.setDate(birthdayDay);

    birthday.setMonth(birthdayMonth-1);      // set birthday month to December
    birthday.setDate(birthdayDay);         // set birthday date to the 15th

if (today > birthday)
{
    // If the B'day is less than current day means you are refering to the next B'day
    birthday.setYear(today.getFullYear() + 1); // Set Year = current year + 1
}

diff = Math.abs(birthday.getTime( ) - today.getTime( ));
diff = Math.floor(diff / (1000 * 60 * 60 * 24));
alert('There are ' + diff + ' days until ' + birthday);

And, here's the JS Fiddle

Upvotes: 0

Sridhar DD
Sridhar DD

Reputation: 1980

Probably you are not setting the year if his birthday had gone past this year.

Let me say today is 28 October, All other birthday date > 28 && month > 10 will work. But It will not work for the past month and past dates. So if the birth day already crossed in this year you should have to add 1 to the year so that it will check for next Birthday(ie. Next Year).

today = new Date( );          // set today's date
birthday = new Date( );      // set up the Birthday object
birthday.setMonth(11);      // set birthday month to December
birthday.setDate(22);         // set birthday date to the 15th
if (today.getTime( ) >= birthday.getTime( )) //Add current year+1 if birthday already reached for this year
{
   birthday.setYear(today.getYear()+1);
}
if (today.getTime( ) < birthday.getTime( ))
{  diff = birthday.getTime( ) - today.getTime( );
   diff = Math.floor(diff / (1000 * 60 * 60 * 24));
   document.write('There are ' + diff + ' days until ' + (birthdayMonth+1)+ ' ' + birthdayDay);
}

Upvotes: 0

Sam
Sam

Reputation: 2917

Try this

var birthdayMonth = document.getElementById('selMonth').value;
var birthdayDay = document.getElementById('selDay').value;

birthdayMonth = parseInt(birthdayMonth);
birthdayDay = parseInt(birthdayDay);

    today = new Date( );          // set today's date
    birthday = new Date( );      // set up the Birthday object
    birthday.setMonth(birthdayMonth-1);      // set birthday month to December
    birthday.setDate(birthdayDay);         // set birthday date to the 15th

if (today < birthday)
 {  
     diff = Math.abs(birthday.getTime( ) - today.getTime( ));
     diff = Math.floor(diff / (1000 * 60 * 60 * 24));
     alert('There are ' + diff + ' days until ' + (birthdayMonth)+ ' ' + birthdayDay);
  }
else
{
    alert("B'day has passed!");
}

And here's the JS Fiddle

Hope this helped!

Upvotes: 1

Related Questions