请问本时在论坛评论上面看到的“于1月前说 ”“于4分钟前说”代码怎样写
------解决方案--------------------------------------------------------
- C# code
public class CalculationTimes { public static string CalculationTime(DateTime Starttime) { string timeString = ""; DateTime EndTime = DateTime.Now; if (EndTime < Starttime) return "开始时间不能大于当前时间"; TimeSpan span = EndTime - Starttime; if (span.Days > 365) timeString = (span.Days / 365).ToString() + "年前"; else if (span.Days > 30) timeString = (span.Days / 30).ToString() + "个月前"; else if (span.Days > 0) timeString = span.Days.ToString() + "天前"; else if (span.Hours > 0) timeString = span.Hours.ToString() + "小时前"; else if (span.Minutes > 0) timeString = span.Minutes.ToString() + "分钟前"; else if (span.Seconds > 0) timeString = span.Seconds.ToString() + "秒前"; else timeString = "刚刚"; return timeString; } }