public static string Since(System.DateTime now, System.DateTime last)
{
string strResult = "";
try
{
TimeSpan ts = new TimeSpan();
ts = now.Subtract(last);
string strNumber = "";
if (ts.TotalSeconds <= 30)
{
strResult = "刚刚";
}
else if (ts.TotalSeconds <= 59)
{
strResult = "1分钟前";
}
else if (ts.TotalMinutes <= 60 && ts.TotalMinutes != 30)
{
strNumber = Convert.ToInt32(ts.TotalMinutes).ToString();
if (ts.TotalMinutes == 2)
{
strNumber = "两";
}
strResult = strNumber + "分钟前";
}
else if (ts.TotalMinutes == 30)
{
strResult = "半小时前";
}
else if (ts.TotalHours <= 23)
{
strNumber = Convert.ToInt32(ts.TotalHours).ToString();
if (ts.TotalHours == 2)
{
strNumber = "两";
}
strResult = ts.Hours + "小时前";
}
else if (ts.TotalHours == 24)
{
strResult = "一天前";
}
else if (ts.TotalDays < 30 && ts.TotalDays != 15)
{
strNumber = Convert.ToInt32(ts.TotalDays).ToString();
if (ts.TotalDays == 2)
{
strNumber = "两";
}
strResult = strNumber + "天前";
}
else if (ts.TotalDays == 15)
{
strResult = "半个月前";
}
else if (ts.TotalDays <= 364)
{
int month = Convert.ToInt32(ts.TotalDays) / 30;
strNumber = month.ToString();
if (month == 2)
{
strNumber = "两";
}
strResult = strNumber + "个月前";
}
else if (ts.TotalDays <= 547)
{
strResult = "一年前";
}
else if (ts.TotalDays <= 910)
{
strResult = "两年前";
}
else
{
strResult = last.ToString("yy年M月d日");
}
}
catch (Exception ex)
{
YtRuntime.WriteLog(ex.ToString());
strResult = last.ToString("yy年M月d日");
}
return strResult;
}
public static string Since(System.DateTime last)
{
return Since(System.DateTime.Now, last);
}