Arthur Fuzaro Arthur Fuzaro - 3 months ago 13
C# Question

Compare DateTime.Now with a custom Date

I want to know how I Should compare dateTime.now with my custom time, for example I want to know if the current time is in the middle of

09:00
and
18:00
.

It's Simple, but I not getting how should I Compare this times.

Thanks a lot

Answer

You can do by this

DateTime now = DateTime.Now;
DateTime d1 = DateTime.ParseExact(now.ToString("dd/MM/yyyy")+ " 09:00", "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

if (now > d1)
{
    Console.WriteLine("Greater");
}