jQuery / Javascript计算月份,星期,天,小时,分钟和秒中两个日期之间的差异

时间:2019-02-17 10:33:13

标签: javascript jquery html

我想计算JavaScript中两个日期之间在月,周,天,小时,分钟和秒之间的差异。

问题: 周和天计算不正确。

我已经尝试将.get...()更改为.getUTC...(),但两者的差额也计算错了。

        var date = new Date("{% if holiday.is_now %}{{ holiday.end_date.isoformat }}{% else %}{{ holiday.end_date.isoformat }}{% endif %}");

        function calcDate(a, b) {
            var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDay(), a.getHours(), a.getMinutes(), a.getSeconds());
            var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDay(), b.getHours(), b.getMinutes(), b.getSeconds());

            return (utc2 - utc1) / 1000;
        }

        function convertDate(seconds){
            var sec = Math.floor(seconds % 60);
            var min = Math.floor(seconds / 60 % 60);
            var hour = Math.floor(seconds / 60 / 60 % 24);

            var diff = seconds / 60 / 60 / 24;
            var months = Math.floor(diff / 30);
            var weeks = Math.floor(diff / 7 % (30 / 7));
            var days = Math.floor(diff % 7);
            console.log(days);

            return [months, weeks, days, hour, min, sec]
        }

        function add_countdown(sec){
            $.each(convertDate(sec), function(i, element){
                var selected = $("footer .countdown .counter#_counter_date_" + i);
                selected.find("h1").text(element);
                singular_pluralize(selected.find("p"), element);
            })
        }

        function singular_pluralize(element, integer){
            integer > 1 || integer == 0 ? element.text(element.attr("data-word-plural")) : element.text(element.attr("data-word-singular"));
        }

        var interval;
        $("footer table td.a").on("click mouseup", function(){
            clearInterval(interval);
            date = new Date($(this).attr("data-date"));
            $("footer #_foter_big_countdown_to_what").text("zu den " + $(this).attr("data-name").replace(/ /g, ''));
            set_interval();
        })

        function set_interval(){
            add_countdown(calcDate(new Date(), date));
            interval = window.setInterval(function(){
                var calc = calcDate(new Date(), date);
                if (calc == 0)
                    holiday_begin();
                else
                    add_countdown(calc);
            }, 900);
        }

        function holiday_begin(){
            $("footer .counter, footer .part#_footer_select_holiday").remove();
            $("footer .darken h1._footer_big_countdown").html("Fröhliche Ferien!");
        }

        set_interval();

编辑:

我找到了解决方案。我必须使用Math.round,并且必须进行一些更改:

        function convertDate(seconds){
            var sec = Math.round(seconds % 60);
            var min = Math.round(seconds / 60 % 60);
            var hour = Math.round(seconds / 60 / 60 % 24);

            var diff = seconds / 60 / 60 / 24;
            var months = Math.round(diff / 30);
            var days = Math.round(diff % 30);
            var weeks = Math.round(months / 4.3);


            return [months, weeks, days, hour, min, sec]
        }

0 个答案:

没有答案