开发者

time like facebook style(..min ago)

开发者 https://www.devze.com 2022-12-29 03:29 出处:网络
Does anyone have a idea,how to use date/time function to write time like facebook/twitter style: e.g. 5 min ago

Does anyone have a idea,how to use date/time function to write time like facebook/twitter style: e.g. 5 min ago e.g. 10 hours ago.

Does anyone have tutorial for asp.net开发者_运维问答..


If you are willing to do the time formatting on the client side, there's a jQuery plugin called timeago that does this.


  public static class DateExtension {
    public static string TimeAgo(this DateTime date) {

     TimeSpan timeSince = DateTime.Now.Subtract(date);

     if (timeSince.TotalMilliseconds < 1)
        return "not yet";
     if (timeSince.TotalMinutes < 1)
        return "just now";
     if (timeSince.TotalMinutes < 2)
        return "1 minute ago";
     if (timeSince.TotalMinutes < 60)
        return string.Format("{0} minutes ago", timeSince.Minutes);
     if (timeSince.TotalMinutes < 120)
        return "1 hour ago";
     if (timeSince.TotalHours < 24)
        return string.Format("{0} hours ago", timeSince.Hours);
     if (timeSince.TotalDays == 1)
        return "yesterday";
     if (timeSince.TotalDays < 7)
        return string.Format("{0} days ago", timeSince.Days);
     if (timeSince.TotalDays < 14)
        return "last week";
     if (timeSince.TotalDays < 21)
        return "2 weeks ago";
     if (timeSince.TotalDays < 28)
        return "3 weeks ago";
     if (timeSince.TotalDays < 60)
        return "last month";
     if (timeSince.TotalDays < 365)
        return string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30));
     if (timeSince.TotalDays < 730)
        return "last year";

     //last but not least...
     return string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365));

     }
  }

Originally from http://www.blog.ingenuitynow.net/Extension+Method+For+DateTime+TimeAgo.aspx


I have implemented a similar code on asp.net and I have start from this example.

http://www.codeproject.com/KB/datetime/DateDurationCalculation1.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消