[英]cannot find amount of days until next birthday for the user input
//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');
}
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.