这个代码时间计算是错误的,求修正计算
求》修正时间计算,这个代码时间计算是错误的function lovetime() {
window.setTimeout("lovetime()", 1000);
var seconds = 1000
var minutes = seconds * 60
var hours = minutes * 60
var days = hours * 24
var years = days * 365
var today = new Date()
var todayYear = today.getFullYear()
var todayMonth = today.getMonth()
var todayDate = today.getDate()
var todayHour = today.getHours()
var todayMinute = today.getMinutes()
var todaySecond = today.getSeconds()
// 修改这里时间即可 我的是2018.11.19
var t1 = Date.UTC(2020, 5, 23, 00, 00, 00)
var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond)
var diff = t2 - t1
var diffYears = Math.floor(diff / years)
var diffDays = Math.floor((diff / days) - diffYears * 365)
var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours)
var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes)
var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes *
minutes) / seconds)
document.getElementById("lovetime").innerHTML = "我们已经在一起 " + diffYears + "年" + diffDays + "天" +
diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒啦"
}
lovetime()
跑了下代码,用webstorm跑的, var t1 = Date.UTC(2020, 5, 23, 00, 00, 00)在我这直接报错了。每年直接定死了365天计算也是会存在误差的。你可以参考下这个代码
function GetDateDiff(startTime, endTime, diffType) {
//将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
startTime = startTime.replace(/\-/g, "/");
endTime = endTime.replace(/\-/g, "/");
//将计算间隔类性字符转换为小写
diffType = diffType.toLowerCase();
var sTime =new Date(startTime); //开始时间
var eTime =new Date(endTime); //结束时间
//作为除数的数字
var timeType =1;
switch (diffType) {
case"second":
timeType =1000;
break;
case"minute":
timeType =1000*60;
break;
case"hour":
timeType =1000*3600;
break;
case"day":
timeType =1000*3600*24;
break;
default:
break;
}
return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}
alert(GetDateDiff("2018-11-19 10:30:22","2020-08-23 10:38:22","day")); 轩宸 发表于 2020-8-23 23:27
跑了下代码,用webstorm跑的, var t1 = Date.UTC(2020, 5, 23, 00, 00, 00)在我这直接报错了。每年直接定 ...
大神,我是小白,无法做参考。
能帮我改一下吗? 本帖最后由 轩宸 于 2020-8-24 11:01 编辑
eeeyn 发表于 2020-8-24 09:37
大神,我是小白,无法做参考。
能帮我改一下吗?
javascript 代码
/**
*@param {number} startTime 时间格式 yyyy-MM-dd HH:mm:ss
* @param {number} endTime
*/
function timeDifference(startTime, endTime) { //可以传日期时间或时间戳
endTime = endTime === undefined ? new Date().getTime() : endTime
var start = typeof (startTime) == "number" ? startTime : new Date(startTime).getTime(),
end = typeof (endTime) == "number" ? endTime : new Date(endTime).getTime(),
difference = end - start, //时间差的毫秒数
days = Math.floor(difference / (24 * 3600 * 1000)), //计算出相差天数
leave1 = difference % (24 * 3600 * 1000), //计算天数后剩余的毫秒数
hours = Math.floor(leave1 / (3600 * 1000)), //计算相差分钟数
leave2 = leave1 % (3600 * 1000), //计算小时数后剩余的毫秒数
minutes = Math.floor(leave2 / (60 * 1000)), //计算相差秒数
leave3 = leave2 % (60 * 1000), //计算分钟数后剩余的毫秒数
seconds = Math.round(leave3 / 1000);
console.log(end)
return `${days}天${hours}小时${minutes}分钟${seconds}秒`
}
function runsecond(startTime, endTime) { //传Number 以秒为单位
var timer = setInterval(function () {
let timeDifference1 = timeDifference(startTime);
document.getElementById("lovetime").innerHTML = "我们已经在一起" + timeDifference1 + "啦!"
}, 1000)
}
runsecond("2018-11-19 00:00:00") 轩宸 发表于 2020-8-24 11:00
javascript 代码
/**
*@param {number} startTime 时间格式 yyyy-M ...
谢谢大神。
页:
[1]