Simple way to handle time in C#?

前端 未结 4 1497
囚心锁ツ
囚心锁ツ 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:13

    It's not at all clear what you mean by "is greater than startHour"... but taking

    TimeSpan startHour = new TimeSpan(12, 25, 43);
    if (endHour.TimeOfDay > startHour)
    {
        ...
    }
    

    ... works pretty simply.

    By all means add argument checking to make sure that you don't specify a value for startHour which is < 0 or > 23 hours, but that's all pretty easy.

    .NET's date and time API is quite primitive (even in 3.5) compared with, say, Joda Time - but in this particular case I think it's not too bad.

提交回复
热议问题