zoukankan      html  css  js  c++  java
  • 计算运行时长

            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;
            }
  • 相关阅读:
    var、let、const
    面向女朋友自我介绍
    ES6——class
    一个错误引发的——对异步回调与for循环(小白错误,大神勿进)
    关于this
    关于作用域
    HTML5 8
    HTML5 7
    HTML5 6
    HTML5 4
  • 原文地址:https://www.cnblogs.com/myesn/p/compute-tick-count.html
Copyright © 2011-2022 走看看