gpt4 book ai didi

javascript计算从生日算起的生存天数

转载 作者:行者123 更新时间:2023-12-02 17:30:29 27 4
gpt4 key购买 nike

我正在尝试制作一个脚本来计算您的生命周期。我的想法是用户通过单击按钮来选择他们的生日。我读了一些脚本并写了一些问题,最后一个好人给我发送了这段代码,但它对我有用.. JSFIDDLE

function IncrementDay(month,year)
{
var lastDay = new Date(year, month, 0).getDate();
var nDay=document.getElementById("bday").value;
++nDay;
if (nDay > lastDay) {
nDay =1;
}
document.getElementById("bday").value=nDay;
}

function IncrementMonth(from_IncrementDay = false)
{
var nMonth = document.getElementById("bmonth").value;
++nMonth;
if (nMonth==13) {
nMonth =1;
}
document.getElementById("bmonth").value=nMonth;
}

function isValidDate(s) {
var bits = s.split('/');
var y = bits[0], m = bits[1], d = bits[2];
// Assume not leap year by default (note zero index for Jan)
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];

// If evenly divisible by 4 and not evenly divisible by 100,
// or is evenly divisible by 400, then a leap year
if ( (!(y % 4) && y % 100) || !(y % 400)) {
daysInMonth[1] = 29;
}
return d <= daysInMonth[--m]
}

function days_between(date1, date2) {

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()

// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)

// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)

}

function calculate() {
var _bd = document.getElementById('byear').value + "/" + document.getElementById('bmonth').value + "/" + document.getElementById('bday').value;
if (!isValidDate(_bd)) return;
var _days = days_between(new Date(), new Date(_bd));
document.getElementById("days").innerHTML = _days;
}

var cDate= new Date();
var cDay = cDate.getDate();
var cMonth = cDate.getMonth();
var cYear = cDate.getFullYear();
var days_gone = 0;

++cMonth;

document.getElementById("bday").value=cDay;
document.getElementById("bmonth").value=cMonth;
document.getElementById("byear").value=cYear;

我对javascript不太熟悉,你能告诉我哪里错了吗?谢谢。

最佳答案

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date();

var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));

关于javascript计算从生日算起的生存天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23114322/

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