JavaScript 中的倒计时

发布于 2024-10-19 07:40:12 字数 344 浏览 2 评论 0原文

我想在我们公司的内联网页面上显示距离下一个发薪日的天数,但是 - 发薪日日期不是每 4 周一次,等等。它们将与此类似:

1st January 2011
15th February 2011
12th March 2011
20th April 2011
...

Is it possible to have a javascript countdown Clock that has上面列出了日期,所以一旦一个日期过去了,它就会开始倒计时,直到下一个日历日期?

我可以找到很多倒计时直到特定日期的脚本示例,但没有一个在第一个日期过去后开始倒计时到第二个日期。

谢谢, 担

I would like to put on our company intranet page the number of days until the next payday, however - the payday dates aren't every 4 weeks etc. they will be similar to this:

1st January 2011
15th February 2011
12th March 2011
20th April 2011
...

Is it possible to have a javascript countdown clock that has the above dates listed, so once one date has passed it would then start counting down until the next calendar date?

I can find plenty of examples of scripts that countdown until a specific date but none that start counting down to the second date once the first has passed.

Thanks,
Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-10-26 07:40:12

将日期放入数组中。请注意,在 Javascript 中,月份是从零开始的,范围是 0 到 11。迭代数组,当日期较大时,则显示今天之间的天数:

    var calcPayDate = function () {
        var payDates = [];
        payDates.push(new Date(2011, 0, 1));
        payDates.push(new Date(2011, 1, 15));
        payDates.push(new Date(2011, 2, 12));
        payDates.push(new Date(2011, 3, 20));

        var today = new Date();
        for (var i = 0; i < payDates.length; i++) {
            if (payDates[i] > today) {
                document.getElementById('countdownDiv').innerHTML = calcDays(payDates[i], today);
                break;
            }
        }
    }

    var calcDays = function(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)

    }

calcDays 函数是在 this site

这些天数被放置在一个名为“countdownDiv”的 div 中。

Put the dates in an array. Be careful, in Javascript the months are zero-based so ranging from 0 to 11. Iterate the array and when the date is bigger then today display the days in between:

    var calcPayDate = function () {
        var payDates = [];
        payDates.push(new Date(2011, 0, 1));
        payDates.push(new Date(2011, 1, 15));
        payDates.push(new Date(2011, 2, 12));
        payDates.push(new Date(2011, 3, 20));

        var today = new Date();
        for (var i = 0; i < payDates.length; i++) {
            if (payDates[i] > today) {
                document.getElementById('countdownDiv').innerHTML = calcDays(payDates[i], today);
                break;
            }
        }
    }

    var calcDays = function(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)

    }

The calcDays function is an function found on this site

The days are put in a div which is called 'countdownDiv'.

菩提树下叶撕阳。 2024-10-26 07:40:12

在网络上搜索“JavaScript 教程”。

同时,这里有一些代码可以帮助您入门:

var dates = [
    new Date(2011, 0,  1),  // note that format is year, month-1, day
    new Date(2011, 1, 15),  // don't ask me why
    new Date(2011, 2, 12),
    new Date(2011, 3, 20)
];

var now = new Date();

for (var i in dates) {  // this is a foreach loop
    if (now < dates[i]) {
        document.write(Math.ceil((dates[i] - now) / 86400000));
        break;
    }
}

Search the web for "JavaScript tutorial".

Meanwhile, here's some code to get you started:

var dates = [
    new Date(2011, 0,  1),  // note that format is year, month-1, day
    new Date(2011, 1, 15),  // don't ask me why
    new Date(2011, 2, 12),
    new Date(2011, 3, 20)
];

var now = new Date();

for (var i in dates) {  // this is a foreach loop
    if (now < dates[i]) {
        document.write(Math.ceil((dates[i] - now) / 86400000));
        break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文