根据某一日期计算距今时间
- .Net
- 2020-07-16
- 22热度
- 0评论
代码如下:
using System;
namespace Helper
{
public class DateHelper
{
/// <summary>
/// 根据某一日期计算距今时间
/// </summary>
/// <param name="oldDateTime"></param>
/// <returns></returns>
public static string GetDateStringFromNow(DateTime oldDateTime)
{
TimeSpan ts = DateTime.Now - oldDateTime;
//if (ts.TotalDays > 365)
//{
// return Math.Floor(ts.TotalDays / 365) + "年前";
//}
//6月前的直接显示日期
if (ts.TotalDays > 180)
{
return oldDateTime.ToString("yyyy-MM-dd");
}
else if (ts.TotalDays > 30)
{
return Math.Floor(ts.TotalDays / 30) + "月前";
}
else if (ts.TotalDays > 7)
{
return Math.Floor(ts.TotalDays / 7) + "周前";
}
else if (ts.TotalDays > 1)
{
return Math.Floor(ts.TotalDays) + "天前";
}
else if (ts.TotalHours > 1)
{
return Math.Floor(ts.TotalHours) + "小时前";
}
else if (ts.TotalMinutes > 1)
{
return Math.Floor(ts.TotalMinutes) + "分钟前";
}
else
{
return Math.Floor(ts.TotalSeconds) + "秒前";
}
}
}
}

鲁ICP备19063141号
鲁公网安备 37010302000824号