C#顯示友好時間格式的代碼
const int SECOND = 1; const int MINUTE = 60 * SECOND; const int HOUR = 60 * MINUTE; const int DAY = 24 * HOUR; const int MONTH = 30 * DAY; if (delta < 0) { return "not yet"; } if (delta < 1 * MINUTE) { return ts.Seconds == 1 ? "1秒前" : ts.Seconds + "秒前"; } if (delta < 2 * MINUTE) { return "1分鐘之前"; } if (delta < 45 * MINUTE) { return ts.Minutes + "分鐘"; } if (delta < 90 * MINUTE) { return "1小時前"; } if (delta < 24 * HOUR) { return ts.Hours + "小時前"; } if (delta < 48 * HOUR) { return "昨天"; } if (delta < 30 * DAY) { return ts.Days + " 天之前"; } if (delta < 12 * MONTH) { int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30)); return months <= 1 ? "一個月之前" : months + "月之前"; } else { int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365)); return years <= 1 ? "一年前" : years + "年前"; }
本文由用戶 afm_dw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!