下面将获得距离这些人生日的天数,如果生日已经过了,它将给你之前的生日
我仍然想知道天气是否可以计算到下一个生日的天数,这需要经过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');
}
试试这个
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!");
}
这是JS Fiddle
希望这能有所帮助!
在这里我已经完成了我试图完成的任务…这将检查距离上次生日还有多少天。这将检查离下一个生日还有多少天。它还将检查今天是否是用户的生日
感谢大家帮助我得出这个结论。在你的帮助下,我得以完成它!我希望这能帮助任何试图做类似事情的人。
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;
如果他的生日已经过了,你可能没有设置年份。
让我说今天是10月28日,所有其他的生日日期>28&;月>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);
}
如果您需要计算截至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);
这是JS Fiddle