我正在使用下面的代码来计算 monotouch 中通知的时间戳。dateAdded
参数是DateTime
来自服务器的 notificationDatetimeUTC
格式。但是“时间”即 TimeSpan is getting negative sometimes because
dateAdded value is getting greater than
DateTime.UtcNow`,这是错误的。那么,如何在monotouch中解决这个问题。
代码:
public static string GetTimeStamp (this DateTime dateAdded) {
TimeSpan time = DateTime.UtcNow - dateAdded;
if (time.TotalDays > 7)
return string.Format ("on {0}", dateAdded.ToLocalTime ().ToString ("MMM dd, yyyy 'at' hh:mm tt"));
if (time.TotalHours > 24)
return string.Format ("about {0} day{1} ago", time.Days, time.Days == 1 ? "" : "s");
if (time.TotalMinutes > 60)
return string.Format ("about {0} hour{1} ago", time.Hours, time.Hours == 1 ? "" : "s");
if (time.TotalSeconds > 60)
return string.Format ("about {0} minute{1} ago", time.Minutes, time.Minutes == 1 ? "" : "s");
else if (time.TotalSeconds > 10)
return string.Format ("about {0} second{1} ago", time.Seconds, time.Seconds == 1 ? "" : "s");
else
return "a moment ago";
}