当前位置:网站首页>C # calculate two time intervals
C # calculate two time intervals
2022-06-09 14:31:00 【Xiongsiyu】
Code
/// <summary>
/// Calculate the duration of the two time intervals
/// </summary>
/// <param name="TimeType"> Time type returned </param>
/// <param name="StartTime"> Starting time </param>
/// <param name="EndTime"> End time </param>
/// <returns> Return interval , The time type of the interval depends on the parameter TimeType distinguish </returns>
public static int GetSpanTime(TimeType TimeType, DateTime StartTime, DateTime EndTime)
{
TimeSpan ts1 = new TimeSpan(StartTime.Ticks);
TimeSpan ts2 = new TimeSpan(EndTime.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//TimeSpan ts = EndTime - StartTime;
double result = 0;
switch (TimeType)
{
case TimeType.Seconds:
result = ts.TotalSeconds;
break;
case TimeType.Minutes:
result = ts.TotalMinutes;
break;
case TimeType.Hours:
result = ts.TotalHours;
break;
case TimeType.Days:
result = ts.TotalDays;
break;
}
return Convert.ToInt32(result);
}
/// <summary>
/// Time type
/// </summary>
public enum TimeType
{
/// <summary>
/// second
/// </summary>
Seconds = 0,
/// <summary>
/// minute
/// </summary>
Minutes = 1,
/// <summary>
/// Hours
/// </summary>
Hours = 2,
/// <summary>
/// God
/// </summary>
Days = 3,
/// <summary>
/// month
/// </summary>
Months = 4
}call :
class Program
{
static void Main(string[] args)
{
DateTime dateTime1 = DateTime.Now;
Thread.Sleep(3000);
DateTime dateTime2 = DateTime.Now;
int result = GetSpanTime(TimeType.Seconds, dateTime1, dateTime2);
Console.WriteLine(string.Format("{0} {1}", result, TimeType.Seconds.ToString()));
Console.ReadKey();
}
}Output :
3 Seconds
Reprinted address :C# Calculate two time intervals _ Low profile fat blog -CSDN Blog _c# Calculate the time interval
end
边栏推荐
猜你喜欢
随机推荐
占位智能家居市场,施耐德电气仅靠一个Wiser系统?
TCP/IP协议(1)
【Leetcode刷题记录】分类整理
On the difference between redistemplate and stringredistemplate
小众专业如何解决“成长”的烦恼?
UniswapV2周边合约学习(五)-- ExampleFlashSwap.sol
UniswapV2周边合约学习(八)-- ExampleSwapToPrice.sol
Common image segmentation methods
[leetcode question brushing record] sorting
#yyds干货盘点# 解决剑指offer:矩形覆盖
MySQL数据库(25):外键 foreing key
6000 + words to help you understand the evolution of Internet architecture!
WordPress地址(URL)修改后打不开网站的解决方法
#导入Word文档图片# VM虚拟机网络设置
Cloud native essay kubernetes workload
【深度优先搜索】玩具蛇:迷宫问题
临界区、事件、互斥量、 信号量--四种控制多线程同步与互斥的方法
使用%UnitTest进行单元测试
工作到一半电脑重启
左右最值最大差








