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#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的方式
這篇文章主要給大家介紹了關(guān)于C#連接Oracle數(shù)據(jù)庫字符串(引入DLL)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08C#實現(xiàn)集合轉(zhuǎn)換成json格式數(shù)據(jù)的方法
這篇文章主要介紹了C#實現(xiàn)集合轉(zhuǎn)換成json格式數(shù)據(jù)的方法,涉及C#針對dataTable、Enumerable及Json格式數(shù)據(jù)的遍歷及轉(zhuǎn)換操作相關(guān)技巧,需要的朋友可以參考下2016-07-07C#根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04