Skip to content

Compare DateTime date or time only

To compare just the time of two DateTimes, you can use the TimeOfDay property:

var one = DateTime.Now.AddHours(1);
var two = DateTime.Now;
var diff = one.TimeOfDay - two.TimeOfDay;

The same code for the date:

var one = DateTime.Now.AddDays(1);
var two = DateTime.Now;
var diff = one.Date - two.Date;

(source)

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.