public string GetTickCount(DateTime? startTime, DateTime? endTime)
{
if (!startTime.HasValue)
return null;
string tickCount = null;
if (!endTime.HasValue)
{
endTime = DateTime.Now;
}
if (startTime.Value > endTime.Value)
throw new ValidationException("开始时间不能大于结束时间");
var totalSeconds = (endTime.Value - startTime.Value).TotalSeconds;
var days = Math.Truncate(totalSeconds / 60 / 60 / 24);
var hours = Math.Truncate(totalSeconds / 60 / 60 % 24);
var minutes = Math.Truncate(totalSeconds / 60 % 60);
var seconds = Math.Truncate(totalSeconds % 60);
if (days > 0)
{
tickCount += $"{days}天";
}
if (hours > 0)
{
tickCount += $"{hours}小时";
}
if (minutes > 0)
{
tickCount += $"{minutes}分钟";
}
if (seconds > 0)
{
tickCount += $"{seconds}秒";
}
return tickCount;
}