Javascript 找不到用户输入的下一个生日之前的天数 下面将得到该人生日前的天数,如果生日已经过了,则会给出你生日前几天的时间! 我仍然在想,是否有可能计算到下一个生日之前的天数,这需要经过12月,并从1月开始继续计算。我似乎做得不对。 //获取生日和月表 var birthdayMonth=document.getElementById('selMonth').value; var birthday=document.getElementById('selDay').value; //解析生日和月份 birthdayMonth=parseInt(birthdayMonth); 生日=parseInt(生日); //设置日期对象 今天=新日期();//确定今天的日期 生日=新日期();//设置生日对象 生日。设定月(生日月);//将生日月份设置为12月 生日。设定日期(生日);//把生日定在15号 如果(今天

Javascript 找不到用户输入的下一个生日之前的天数 下面将得到该人生日前的天数,如果生日已经过了,则会给出你生日前几天的时间! 我仍然在想,是否有可能计算到下一个生日之前的天数,这需要经过12月,并从1月开始继续计算。我似乎做得不对。 //获取生日和月表 var birthdayMonth=document.getElementById('selMonth').value; var birthday=document.getElementById('selDay').value; //解析生日和月份 birthdayMonth=parseInt(birthdayMonth); 生日=parseInt(生日); //设置日期对象 今天=新日期();//确定今天的日期 生日=新日期();//设置生日对象 生日。设定月(生日月);//将生日月份设置为12月 生日。设定日期(生日);//把生日定在15号 如果(今天,javascript,datetime,Javascript,Datetime,试试这个 var birthdayMonth = document.getElementById('selMonth').value; var birthdayDay = document.getElementById('selDay').value; birthdayMonth = parseInt(birthdayMonth); birthdayDay = parseInt(birthdayDay); today = new Date( ); // set to

试试这个

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!");
}
var birthdayMonth=document.getElementById('selMonth').value;
var birthday=document.getElementById('selDay').value;
birthdayMonth=parseInt(birthdayMonth);
生日=parseInt(生日);
今天=新日期();//确定今天的日期
生日=新日期();//设置生日对象
生日。设置月份(生日月份-1);//将生日月份设置为12月
生日。设定日期(生日);//把生日定在15号
如果(今天<生日)
{  
diff=Math.abs(生日.getTime()-today.getTime());
差异=数学楼层(差异/(1000*60*60*24));
警报(“+(生日月)+”''+生日之前有'+diff+'天);
}
其他的
{
警惕(“B’天已经过去了!”);
}


希望这有帮助

如果他的生日已经过了今年,你可能不会定下年份

让我说今天是10月28日,所有其他的生日日期>28&&month>10都可以。 但在过去的一个月和过去的日子里,它将不起作用。因此,如果今年的生日已经过了,你应该在这一年中加上1,这样它就可以检查下一个生日(即明年)

today=新日期();//确定今天的日期
生日=新日期();//设置生日对象
生日。设定月(11);//将生日月份设置为12月
生日。设定日期(22);//把生日定在15号
if(today.getTime()>=birth.getTime())//如果今年的生日已到,则添加当前年份+1
{
生日.setYear(今天.getYear()+1);
}
如果(今天.getTime()<生日.getTime())
{diff=birth.getTime()-today.getTime();
差异=数学楼层(差异/(1000*60*60*24));
文档.写入(“+(生日月+1)+”''+生日之前有'+diff+'天);
}
在这里,我已经完成了我试图完成的任务。。。这将检查距离上次生日有多少天了。这将检查离下一个生日还有多少天。它还将检查用户是否在今天过生日。 谢谢大家帮我得出这个结论。在你的帮助下,我完成了它!我希望这能帮助任何试图做类似事情的人

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;
var输出=”;
var birthdayMonth=document.getElementById('selMonth').value;
var birthday=document.getElementById('selDay').value;
birthdayMonth=parseInt(birthdayMonth);
生日=parseInt(生日);
//日期对象
今天=新日期();//确定今天的日期
生日=新日期();//生日对象设置,例如(生日.getTime());
生日。设定月(生日月);//设置生日月份(用户输入)
生日。设定日期(生日);//设置生日日期(用户输入)
//检查今天是否是用户的生日
如果(生日.valueOf()==今天.valueOf()){
sweetAlert(“生日快乐!”);
}
//直到下一个生日
如果(今天>生日)
{
//如果过了生日,它会计算下一个生日还有多久,即使是明年。
生日.setYear(今天.getFullYear()+1);//设置年份=当前年份+1
diff=Math.abs(生日.getTime()-today.getTime());
差异=数学楼层(差异/(1000*60*60*24));
//提醒('离您的生日还有'+diff+'天'/*将此添加为实际日期->生日*/);提醒您还有多少天

输出+='离生日还有'+diff+'天'/*将此添加到实际日期->生日如果您需要计算到B'天的日期,而不考虑年份,这里是答案

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

您的问题是什么?在尝试使用
birthdayMonth
birthdayDay
之前,您已经尝试过记录它们的值了吗?@winterblood我有一个表单,用户可以在其中选择出生日期!我已经尝试过多次获取.value(表示特定日期的数字)(例如:2月)请记住,在Javascript中,月份是0索引的(在setDate中,december是11),如果您的HTML中有值为2的二月,则在使用它之前,您必须先减少birthdayMonth。还请记住,您没有在代码中设置年份-如果生日小于或等于今天的日期,它将尝试计算输入的2014年日期,因此不会将其输入
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;
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);