Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# – How can I check if DateTime has time

How can I validate if a give DateTime has actual time not just the default midnight time (00:00:00).

DateTime.TryParse("2022-11-01T14:52:17", out DateTime withTime1); // has time
DateTime.TryParse("2022-11-01T00:00:01", out DateTime withTime2); // has time
DateTime.TryParse("2022-11-01T00:00:00", out DateTime noTime ); // doesn't have time

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

None of what you posted are DateTime values. They are strings. I’ll answer the question you actually asked though:

if (myDateTime.TimeOfDay == TimeSpan.Zero)
{
    // Time is midnight.
}
else
{
    // Time is not midnight.
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading