c#友好顯示日期 c#日期datetime使用方法
更新時間:2014年01月16日 11:06:50 作者:
c# datetime根據(jù)日期顯示幾秒前、幾分前、幾天前、幾月前、幾年前等格式
復(fù)制代碼 代碼如下:
#region 友好顯示時間
/// <summary>
/// 友好顯示時間
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static string ShowTime(DateTime date)
{
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;
TimeSpan ts = DateTime.Now - date;
double delta = ts.TotalSeconds;
if (delta < 0)
{
return "not yet";
}
if (delta < 1 * MINUTE)
{
return ts.Seconds == 1 ? "1秒前" : ts.Seconds + "秒前";
}
if (delta < 2 * MINUTE)
{
return "1分鐘之前";
}
if (delta < 45 * MINUTE)
{
return ts.Minutes + "分鐘";
}
if (delta < 90 * MINUTE)
{
return "1小時前";
}
if (delta < 24 * HOUR)
{
return ts.Hours + "小時前";
}
if (delta < 48 * HOUR)
{
return "昨天";
}
if (delta < 30 * DAY)
{
return ts.Days + " 天之前";
}
if (delta < 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "一個月之前" : months + "月之前";
}
else
{
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "一年前" : years + "年前";
}
}
#endregion
您可能感興趣的文章:
- c# datetime方法應(yīng)用介紹
- C#時間格式化(Datetime)用法詳解
- 關(guān)于C#中DateTime常用方法概述
- c#中DateTime.Now函數(shù)的使用詳解
- 深入Unix時間戳與C# DateTime時間類型互換的詳解
- c# DateTime常用操作實例(datetime計算時間差)
- c# datetime 格式化大全
- c#詳解datetime使用示例
- C#中DateTime日期類型格式化顯示方法匯總
- C#日期控件datetimepicker保存空值的三種方法
- C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
- C#中比較常用的DateTime結(jié)構(gòu)的使用方法
- C# DateTime.ToString根據(jù)不同語言生成相應(yīng)的時間格式
- C# 中DateTime 的使用技巧匯總
相關(guān)文章
C# 控件屬性和InitializeComponent()關(guān)系案例詳解
這篇文章主要介紹了C# 控件屬性和InitializeComponent()關(guān)系案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

