手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#友好显示日期 c#日期datetime使用方法
c#友好显示日期 c#日期datetime使用方法
摘要:复制代码代码如下:#region友好显示时间//////友好显示时间/////////publicstaticstringShowTime(...

复制代码 代码如下:

#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#友好显示日期 c#日期datetime使用方法】相关文章:

C#实现窗体淡入淡出效果的方法总结

c#进度条 progressBar 使用方法的小例子

C# 设置系统日期格式的方法

string类的使用方法详解

C#中控制远程计算机的服务的方法

c# 获取数据库中所有表名称的方法

c#中DateTime.Now函数的使用详解

c#实现隐藏与显示任务栏的方法详解

C# 常用日期时间函数(老用不熟)

c#中SAPI使用总结——SpVoice的使用方法

精品推荐
分类导航