首页 > c# > 使用JQuery Timeago显示的日期时间

使用JQuery Timeago显示的日期时间 (Datetime displayed using JQuery Timeago)

问题

我正在尝试使用timeago插件显示我的日期。这是我到目前为止:

<div class="container col-lg-12 col-md-12 col-xs-12 notifications-container">
@{
    foreach (var n in Model.Notifications)
    {
        <div class="row">
            <a href="@n.URL">
                 <div class="container col-lg-8 col-md-10 col-xs-12">
                    <h2>@n.Title</h2>
                    <h5>@n.ShortDescription</h5>
                    <h5 id="date-time"></h5>
                    <h5>
                        <time class="timeago" datetime="@n.Time.ToString("s", System.Globalization.CultureInfo.InvariantCulture)"> @n.Time </time>
                    </h5>
                    <script>

                        $(document).ready(function () {
                            $("time.timeago").timeago();
                        });
                    </script>
                </div>
            </a>
        </div>
    }
}

它没有转换我的日期时间,不确定我是否需要更改它的格式,以便timeago可以使用它。

更新:我得到了这种方式,在这种情况下传递的DateTime是UTC.Now类型

有一个我尚未修复的错误,它表明所有条目都是在一个小时前完成的,尽管其中一些是几秒钟之前的。

解决方法

我们需要将添加到您的项目中作为解决方法,因为,只适用于ISO 8601格式的日期:

你需要改变这个:

<h5 id="date-time">@n.Time</h5>

对此:

<h5 class="date-time">
 <time class="timeago" datetime="">@n.Time</time>
</h5>

和你的javascript代码,这个:

$(document).ready(function() {
  var dateAux;
  $(".timeago").each(function(i,item){
      dateAux = moment($(item).html(),'DD-MM-YYYY hh:mm:ss');
      $(item).attr('datetime',dateAux.toISOString());
  })  
  $("time.timeago").timeago();
});

工作小提琴:https//jsfiddle.net/jn233dk4/1/

问题

I am trying to display my date using the timeago plugin. This is what I have so far:

<div class="container col-lg-12 col-md-12 col-xs-12 notifications-container">
@{
    foreach (var n in Model.Notifications)
    {
        <div class="row">
            <a href="@n.URL">
                 <div class="container col-lg-8 col-md-10 col-xs-12">
                    <h2>@n.Title</h2>
                    <h5>@n.ShortDescription</h5>
                    <h5 id="date-time"></h5>
                    <h5>
                        <time class="timeago" datetime="@n.Time.ToString("s", System.Globalization.CultureInfo.InvariantCulture)"> @n.Time </time>
                    </h5>
                    <script>

                        $(document).ready(function () {
                            $("time.timeago").timeago();
                        });
                    </script>
                </div>
            </a>
        </div>
    }
}

It isn't converting my datetime, Not sure if I need to change the format of it so timeago can use it.

Update: I got it working this way and the DateTime passed in this scenario is of type UTC.Now

There is one bug that I have yet to fix which shows that all entries were made about an hour ago even though some of them were a couple of seconds ago..

解决方法

We need to add to your project as a workaround, because, just works with dates in ISO 8601 format:

You need to change this:

<h5 id="date-time">@n.Time</h5>

To this:

<h5 class="date-time">
 <time class="timeago" datetime="">@n.Time</time>
</h5>

And your javascript code, to this one:

$(document).ready(function() {
  var dateAux;
  $(".timeago").each(function(i,item){
      dateAux = moment($(item).html(),'DD-MM-YYYY hh:mm:ss');
      $(item).attr('datetime',dateAux.toISOString());
  })  
  $("time.timeago").timeago();
});

Working fiddle: https://jsfiddle.net/jn233dk4/1/

相似信息