简体   繁体   English

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

[英]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. 我仍然想知道天气是否有可能计算直到下一个生日的天数,这将需要超过12月并继续从1月开始计数。 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');

}

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;

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 这是JS小提琴

Hope this helped! 希望这对您有所帮助!

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. 我想说今天是10月28日,所有其他生日> 28 && month> 10都可以使用。 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). 因此,如果今年的出生日期已经过了,那么您应该在该年份加上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);
}

Here's the answer if you need to calculate dates to B'day irrespective of the year 这是答案,如果您需要计算与年份无关的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 而且,这是JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM