找不到用户输入的下一个生日的天数

时间:2014-10-28 03:32:32

标签: javascript datetime

以下将获得人们生日前的天数,如果生日已经过去,那么它会告诉你生日那天的前几天!

我仍然想知道天气可以计算到下一个生日的天数,这需要经过12月并继续从1月开始计算。我似乎无法做对。

 //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');

}

4 个答案:

答案 0 :(得分:1)

在这里,我完成了我要做的任务......这将 检查自上次生日以来的天数 。这将 检查下一个生日的天数 。它还 检查今天是否为用户生日

感谢大家帮助我得出这个结论。在你的帮助下,我完成了它!我希望这有助于任何人尝试做类似的事情。

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;

答案 1 :(得分:0)

试试这个

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

希望这有帮助!

答案 2 :(得分:0)

如果他的生日已经过了一年,你可能没有设定年份。

让我说今天是10月28日,所有其他生日日期&gt; 28&amp;&amp;月份&gt; 10会奏效。 但它不适用于过去一个月和过去的日期。因此,如果在今年已经过了分娩日,你应该在一年中加1,以便检查下一个生日(即明年)。

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);
}

答案 3 :(得分:0)

如果您需要计算B'day的日期,无论年份如何,这都是答案

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