using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LifePayment.Domain.Shared; public static class TimeUtility { /// <summary> /// 获å–过去多长时间å—符串 /// ToDoåŽç»å¯ä»¥ä¼˜åŒ–ä¸ºæ ¹æ®æŒ‡å®šçš„æ—¶é—´é—´é𔿖‡æœ¬å—å…¸æ¥æ›´åŠ çµæ´»çš„返回对应的文本 /// </summary> /// <param name="sourceTime"></param> /// <param name="now"></param> /// <returns></returns> public static string GetBeforeTimeStr(DateTime sourceTime, DateTime? now = null) { string result = null; now = now ?? DateTime.Now; if (now < sourceTime) { return result; } var timeSpan = now.Value.Date - sourceTime.Date; switch (timeSpan.TotalDays) { case >= 1 and < 2: result = $"昨天"; break; case >= 2 and < 3: result = $"å‰å¤©"; break; case >= 3 and < 30: result = $"{Math.Floor(timeSpan.TotalDays)}天å‰"; break; case >= 30: result = $"{Math.Floor(timeSpan.TotalDays / 30)}月å‰"; break; default: timeSpan = now.Value - sourceTime; if (timeSpan.TotalHours >= 1) { result = $"{Math.Floor(timeSpan.TotalHours)}å°æ—¶å‰"; } else { result = $"刚刚"; } break; } return result; } }