我正在尝试用JavaScript查找秒表/计数计时器,其中包括我网站的天,周,月和年。

我已经尝试过找到它,但是我发现的所有东西只有几分钟和几秒钟。

我发现我喜欢的一点代码是这样的:



var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);

function setTime() {
  ++totalSeconds;
  secondsLabel.innerHTML = pad(totalSeconds % 60);
  minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
}

function pad(val) {
  var valString = val + "";
  if (valString.length < 2) {
    return "0" + valString;
  } else {
    return valString;
  }
}

<label id="minutes">00</label>:<label id="seconds">00</label>





从这个问题:plain count up timer in javascript,但是再次没有几天,几周,几个月和几年。

有人可以重写为几天,几周,几个月和几年吗?

另外,这不是重复的。

最佳答案

请注意,这只是一个简单的示例,而不是确切的示例:



var minutesLabel = document.getElementById("minutes");
    var secondsLabel = document.getElementById("seconds");
    var hours = document.getElementById("hours");
    var days = document.getElementById("days");
    var weeks = document.getElementById("weeks");
    var months = document.getElementById("months");
    var years = document.getElementById("years");
    var totalSeconds =31540000;
    setInterval(setTime, 1000);

    function setTime() {
        --totalSeconds;
        var yearD = parseInt(totalSeconds / (31540000))
        var yearM = parseInt(totalSeconds % (31540000))

        var monthD = parseInt(yearM / (2628336.2137829))
        var monthM = parseInt(yearM % (2628336.2137829))

        var weekD = parseInt(monthM / (604876.71234816079959))
        var weekM = parseInt(monthM % (604876.71234816079959))

        var dayD = parseInt(weekM / (86410.958906880114228))
        var dayM = parseInt(weekM % (86410.958906880114228))

        var hourD = parseInt(dayM / (3600))
        var hourM = parseInt(dayM % (3600))

        var minD = parseInt(hourM / (60))
        var secD = parseInt(hourM % (60))



        secondsLabel.innerHTML = pad(secD);
        minutesLabel.innerHTML = pad(minD);
        hours.innerHTML = pad(hourD);
        days.innerHTML = pad(dayD);
        weeks.innerHTML = pad(weekD);
        months.innerHTML = pad(monthD);
        years.innerHTML = pad(yearD);
    }

    function pad(val) {
        var valString = val + "";
        if (valString.length < 2) {
            return "0" + valString;
        } else {
            return valString;
        }
    }

years<label id="years">00:</label>
    months<label id="months">00:</label>
    weeks<label id="weeks">00:</label>
    days<label id="days">00:</label>
    hours<label id="hours">00</label>
    <label id="minutes">00</label>:<label id="seconds">00</label>

10-08 12:37