Simple way to handle time in C#?

前端 未结 4 1495
囚心锁ツ
囚心锁ツ 2020-12-18 08:01

My current approach is something like

DateTime startHour = new DateTime(1900,1,1,12,25,43);
DateTime endHour = new DateTime(1900,1,1,13,45,32);

// I need to         


        
4条回答
  •  执笔经年
    2020-12-18 08:19

    A little hint - .NET supports arithmetic operations on DateTime objects, and returns a TimeSpan object. Thus, you can do the following:

    DateTime fromDate = ....
    DateTime toDate = ....
    TimeSpan diff = toDate - fromDate;
    

    and you can expand this to:

    DateTime fromDate = DateTime.Now;
    DateTime toDate = DateTime.Now.addMinutes(x);
    
    if ((toDate - fromDate).TotalMinutes > 15) {
        ...
    }
    

提交回复
热议问题