asp.net 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
2013-03-28 14:03
726 查看
/// <summary>
/// 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
/// </summary>
/// <param name="dt">要格式化显示的时间</param>
/// <returns>几个月,几天前,几小时前,几分钟前,或几秒前</returns>
public static 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秒前";
}
}
.codearea{ color:black; background-color:white; line-height:18px; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys","BitStream Vera Sans Mono", courier,monospace,serif}
.codearea pre{ color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap pre{white-space:pre-wrap; white-space:-moz-pre-wrap; white-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word; word-break:normal}
.codearea pre.alt{ background-color:#f7f7ff !important}
.codearea .lnum{color:#4f81bd;line-height:18px}
相关文章推荐
- asp.net 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
- ASP 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
- C# 格式化显示时间为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#和VB.NET把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- C#:把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- C#计算发表时间为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- 把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- C#计算发表时间为几个月,几天前,几小时前,几分钟前,或几秒前
- C#计算发表时间为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- js 显示友好的时间格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- 时间显示 1分钟内显示几秒前,1小时间内显示几分钟前,1天内显示几小时前,7天内显示几天前,其它显示真实时间
- 使用C#把发表的时间改为几年前,几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几年前,几个月,几天前,几小时前,几分钟前,或几秒前
- C#:把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
- 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前