【问题标题】:Display Friendly Date, Numbers [duplicate]显示友好日期,数字 [重复]
【发布时间】:2011-03-03 22:58:48
【问题描述】:

可能重复:
How do I calculate relative time?

我正在为 asp.net 搜索一个自定义控件,它有助于显示用户友好的日期,例如代替文章日期:

(2010 年 4 月 2 日)

它显示

(2 个月大)

我在谷歌上找不到它,请任何人建议链接、自定义控件、文章。

谢谢。

【问题讨论】:

  • @George:那个是用 C# 编写的,这个要求自定义 ASP.NET 控件。我不是 .NET 人,所以我不知道这有多大的不同。
  • @Bill The Lizard 这没什么区别,没有这样的控制,Jeff 的回答中给出了他想做的代码。
  • @George:啊,我还没有向下滚动到足够远的地方看到那个。感谢您为我澄清。

标签: asp.net asp.net-mvc custom-controls


【解决方案1】:

试试这个(这是一个日期时间扩展)。所以用法是:

var date = new DateTime.Now;

string formattedDate = date.ToReadableTimespan();


public static string ToReadableTimespan(this DateTime d)
            {
                // 1.
                // Get time span elapsed since the date.
                TimeSpan s = DateTime.Now.Subtract(d);

                // 2.
                // Get total number of days elapsed.
                int dayDiff = (int)s.TotalDays;

                // 3.
                // Get total number of seconds elapsed.
                int secDiff = (int)s.TotalSeconds;

                // 4.
                // Don't allow out of range values.
                if (dayDiff < 0 || dayDiff >= 31)
                {
                    return null;
                }

                // 5.
                // Handle same-day times.
                if (dayDiff == 0)
                {
                    // A.
                    // Less than one minute ago.
                    if (secDiff < 60)
                    {
                        return "just now";
                    }
                    // B.
                    // Less than 2 minutes ago.
                    if (secDiff < 120)
                    {
                        return "1 minute ago";
                    }
                    // C.
                    // Less than one hour ago.
                    if (secDiff < 3600)
                    {
                        return string.Format("{0} minutes ago",
                            Math.Floor((double)secDiff / 60));
                    }
                    // D.
                    // Less than 2 hours ago.
                    if (secDiff < 7200)
                    {
                        return "1 hour ago";
                    }
                    // E.
                    // Less than one day ago.
                    if (secDiff < 86400)
                    {
                        return string.Format("{0} hours ago",
                            Math.Floor((double)secDiff / 3600));
                    }
                }
                // 6.
                // Handle previous days.
                if (dayDiff == 1)
                {
                    return "yesterday";
                }
                if (dayDiff < 7)
                {
                    return string.Format("{0} days ago",
                        dayDiff);
                }
                if (dayDiff < 31)
                {
                    return string.Format("{0} weeks ago",
                        Math.Ceiling((double)dayDiff / 7));
                }
                return null;
            }

【讨论】:

    【解决方案2】:

    Richard 代码不错。

    我也想提出我使用的这个代码http://www.codeproject.com/KB/datetime/DateDurationCalculation1.aspx

    【讨论】:

      【解决方案3】:

      如果使用 MVC 2,最好的方法 (恕我直言) 是获取 Richard 的代码并通过 DisplayTemplate 将其连接起来(查看此博客以开始使用显示模板:http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html)。

      ...然后您只需使用以下语法从您的视图中调用 DisplayTemplate:

      &lt;% =Html.DisplayFor(model =&gt; model.ArticleDate, "ArticleDateDisplayTemplateName") %&gt;

      我已经使用DisplayTemplates 有一段时间了,它们一直是这类事情的救星,因为您可以在整个应用程序中使用它们。

      【讨论】:

        猜你喜欢
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 1970-01-01
        • 2013-12-15
        • 1970-01-01
        • 2019-07-11
        • 1970-01-01
        相关资源
        最近更新 更多