我正在使用下面的代碼來計算monotouch中通知的時間戳。 dateAdded
參數是DateTime
作爲notificationDatetime來自服務器,格式爲UTC
格式。 但「時間」即TimeSpan is getting negative sometimes because
dateAdded value is getting greater than
DateTime.UtcNow`,這是錯誤的。那麼,如何在monotouch中解決這個問題。DateTime releated issue in ios/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";
}
「_dateadded值變得比DateTime.UtcNow大,這是錯誤的_」 - 我會先解決這個問題。 – neilco