简体   繁体   English

Timeago给我错了时间

[英]Timeago giving me wrong amount of time

I'm trying to use timeago to display how long ago an action was done on my website. 我正在尝试使用timeago来显示我的网站上进行了多长时间的操作。

I'm using an ajax request to add rows to a table, and then once all the rows are added, I'm activating the timeago again. 我正在使用ajax请求将行添加到表中,然后在添加所有行后再次激活timeago。 I have a function called fill_table() which gathers some row data from a php script and then loops through the data adding rows to the table. 我有一个名为fill_table()的函数,该函数从php脚本收集一些行数据,然后循环遍历数据,将行添加到表中。

function fill_table() {
    $.ajax({
        ...
        success : function(data) { 
            var table = $('#table_body');
            table.empty();
            $.each(data, function( index, value ) {

                var date = new Date(new Date(value.last_updates).toLocaleString("en-US", {timeZone: "America/Chicago"})).toISOString();

                var elem = '\
                    <tr>\
                        <td><a target="_blank" href="' + value.name + '">' + value.name + '</a></td>\
                        <td>' + value.market_value + '</td>\
                        <td>' + value.lowest_cost + '</td>\
                        <td>' + value.biggest_md_format + '</td>\
                        <td><time class="timeago" datetime="' + date + '"></time></td>\
                    </tr>\
                ';
                table.append(elem);
            });
            $("time.timeago").timeago();                
        },
        error : function(request,error)
        {
            console.log("Request: "+JSON.stringify(request));
        }
    });
}

Now, for some reason this is telling me "6 hours ago" for timestamps that should only be 8-12 minutes old at the most. 现在,由于某种原因,这告诉我“ 6小时前”的时间戳最长应该只有8-12分钟。

As you can see in the code, I've declared date using some crazy toLocaleString thing just to make sure it was the right timezone, I also already set the PHP header in my controller 正如您在代码中看到的那样,我已经使用一些疯狂的toLocaleString东西声明了date ,以确保它是正确的时区,我也已经在控制器中设置了PHP标头

date_default_timezone_set('America/Chicago');

Inspecting the timeago element gives me this 检查timeago元素可以给我这个

<time class="timeago" datetime="2019-03-03T12:56:28.000Z">about 6 hours ago</time>

Which the datetime is correct, 12:56 AM CST (America/Chcago), but that is only about 10 minutes ago. 正确的日期时间是CST(美国/芝加哥)上午12:56,但仅在10分钟前。

So I'm at a loss, I've tried fixing timezone issues with no luck and don't know what else to do. 所以我很茫然,我试图解决时区问题,但没有运气,也不知道该怎么办。

How can I make timeago read the correct amount of time? 如何让timeago读取正确的时间?

That datetime is not correct. 该日期时间正确。 That Z at the end designates it as UTC time zone, not your local time zone. 最后的Z将其指定为UTC时区,而不是您的本地时区。 So timeago correctly gives you a difference of multiple hours with that time in UTC. 因此,timeago正确地将您与UTC的时间相差了几个小时。

Check your inputs and that conversion logic in JS. 检查您的输入以及JS中的转换逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM