使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前[转]
作者:朱尚 / 日期:2014-02-12 / 分类:C# / 浏览:1494
//使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
//2014年05月15日 星期六 02:35
public string DateStringFromNow(DateTime dt)
{
    TimeSpan span = DateTime.Now - dt;
    if (span.TotalDays > 60)
    {
        return dt.ToShortDateString();
    }
    else
    {
        if (span.TotalDays > 30)
        {
            return
            "1个月前";
        }
        else
        {
            if (span.TotalDays > 14)
            {
                return
                "2周前";
            }
            else
            {
                if (span.TotalDays > 7)
                {
                    return
                    "1周前";
                }
                else
                {
                    if (span.TotalDays > 1)
                    {
                        return
                        string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
                    }
                    else
                    {
                        if (span.TotalHours > 1)
                        {
                            return
                            string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
                        }
                        else
                        {
                            if (span.TotalMinutes > 1)
                            {
                                return
                                string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
                            }
                            else
                            {
                                if (span.TotalSeconds >= 1)
                                {
                                    return
                                    string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
                                }
                                else
                                {
                                    return
                                    "1秒前";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
上一篇:C#排序算法大全
下一篇:asp.net __doPostBack详细解释,用法示例。[转]
本文标签: 几天前 几小时前 几分钟前
本文链接:http://www.banzhuan.net/detail/99