Ask Your Question
4

What is the method to compare Date only in DateTime types without considering the Time?

asked 2021-07-24 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-08-04 10:00:00 +0000

scrum gravatar image

The method to compare Date only in DateTime types without considering the Time is to use the Date property of the DateTime object. This property returns a DateTime object with the time component set to midnight (00:00:00). To compare only the Date component, you can compare the Date property of the two DateTime objects.

For example:

DateTime date1 = new DateTime(2021, 10, 1, 10, 30, 0);
DateTime date2 = new DateTime(2021, 10, 1, 15, 45, 0);

if (date1.Date == date2.Date)
{
    Console.WriteLine("The dates are equal");
}
else
{
    Console.WriteLine("The dates are not equal");
}

In this example, the Date property is used to extract only the date component of the DateTime objects, and the comparison is made based on the dates only, ignoring the time component.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-24 11:00:00 +0000

Seen: 3 times

Last updated: Aug 04 '21