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;
|
}
|
|
}
|