简体   繁体   中英

C# DateTime - How to check Time part is NULL?

Is there any easy way to check to see if the time part of the DateTime value is NULL other than checking hour is 0, min is 0 and sec is 0?

Thanks.

我觉得这很可读:

var isTimeNull = (myDateTime.TimeOfDay == TimeSpan.Zero);

Here are my takes:

var isTimeNull = myDateTime.Date == myDateTime;
var isTimeNull = myDateTime.TimeOfDay.TotalSeconds == 0;

And technically, time is not null, it's just not set.

为避免划分和浮点精度问题,请使用:

var isMidnight = myDateTime.TimeOfDay.Ticks == 0;

DateTime does not support the concept of null for just the time component. Either the whole DateTime object is null (if using the nullable version), or both the date and time are non-null. If you don't specify the time component it will default to 0 (midnight).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM