发贴时间:2008-1-3 11:20:29xxxx 发表帖子 ??????   30分钟前(根据当前时间动态时间换算)60分钟进位到1小时前,24小时进位到天~类推到月-年!!
 
 

解决方案 »

  1.   

    castle monorail里自带两个方法,分别是:
    public String AlternativeFriendlyFormatFromNow(DateTime date),
    public String FriendlyFormatFromNow(DateTime date)代码如下,希望对你有帮助:)
    public String AlternativeFriendlyFormatFromNow(DateTime date)
    {
    TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
    TimeSpan cur = new TimeSpan(date.Ticks);TimeSpan diff = now.Subtract(cur);if (diff.TotalHours <= 24)
    {
    return "Today";
    }
    else if (diff.TotalHours <= 48)
    {
    return "Yesterday";
    }
    else if (diff.TotalDays <= 40)
    {
    return String.Format("{0} days ago", diff.Days);
    }
    else 
    {
    return String.Format("{0} months ago", (diff.Days / 30));
    }
    }/// <summary>
    /// Returns the difference from the 
    /// specified <c>date</c> the the current date
    /// in a friendly string like "1 day ago"
    /// <para>
    /// TODO: Think about i18n
    /// </para>
    /// </summary>
    /// <param name="date">The date in the past (should be equal or less than now)</param>
    /// <returns></returns>
    public String FriendlyFormatFromNow(DateTime date)
    {
    TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
    TimeSpan cur = new TimeSpan(date.Ticks);TimeSpan diff = now.Subtract(cur);if (diff.TotalSeconds == 0)
    {
    return "Just now";
    }if (diff.Days == 0)
    {
    if (diff.Hours == 0)
    {
    if (diff.Minutes == 0)
    {
    return String.Format("{0} second{1} ago", 
    diff.Seconds, diff.Seconds > 1 ? "s" : String.Empty);
    }
    else
    {
    return String.Format("{0} minute{1} ago", 
    diff.Minutes, diff.Minutes > 1 ? "s" : String.Empty);
    }
    }
    else
    {
    return String.Format("{0} hour{1} ago", 
    diff.Hours, diff.Hours > 1 ? "s" :
      

  2.   

    TimeSpan dt = this.dateTime1 - this.dateTime2;
       
     
     再根据dt判断        
      

  3.   

    public String FriendlyFormatFromNow(DateTime date) 
    这个方法最后行代码好象感觉少了嘛..
      

  4.   

        这样子的?
        public String FriendlyFormatFromNow(DateTime date)
        {
            TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan cur = new TimeSpan(date.Ticks);        TimeSpan diff = now.Subtract(cur);        if (diff.TotalSeconds == 0)
            {
                return "Just   now";
            }        if (diff.Days == 0)
            {
                if (diff.Hours == 0)
                {
                    if (diff.Minutes == 0)
                    {
                        return String.Format("{0}   second{1}   ago",
                        diff.Seconds, diff.Seconds > 1 ? "s" : String.Empty);
                    }
                    else
                    {
                        return String.Format("{0}   minute{1}   ago",
                        diff.Minutes, diff.Minutes > 1 ? "s" : String.Empty);
                    }
                }
                else
                {
                    return String.Format("{0}   hour{1}   ago",
                    diff.Hours, diff.Hours > 1 ? "s" : String.Empty);
                }
            }
        }
      

  5.   

      
                DateTime dt = DateTime.Now.AddMinutes(-30);