gpt4 book ai didi

javascript - 无法找到用户输入距下一个生日还有多少天

转载 作者:行者123 更新时间:2023-12-03 11:34:11 24 4
gpt4 key购买 nike

下面将获取距此人生日还有多少天,如果生日已经过,则会给出您生日之前的天数!

我仍然想知道是否可以计算距离下一个生日还有多少天,这需要从 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!");
}

And here's the JS Fiddle

希望这有帮助!

关于javascript - 无法找到用户输入距下一个生日还有多少天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com