Share via


How to compare Time in C#?

Question

Tuesday, September 12, 2006 3:02 AM

Hi all,

I want to compare the system time with 11.00 AM on a button click event.

And if the current time is greater than or equal to 11.00 AM, i've to perform some functions.

I am using C#. Please reply with example code.

Thanks in advance,

Jasmeeta.

All replies (7)

Wednesday, September 13, 2006 1:04 AM

hi all, 

I got it !

DateTime t1 = DateTime.Now;

DateTime t2 = Convert.ToDateTime("11:00:00 AM");

int i = DateTime.Compare(t1,t2);

//if t1 is less than t2 then result is Less than zero

//if t1 equals t2 then result is Zero

//if t1 is greater than t2 then result isGreater zero


Monday, October 2, 2006 3:26 PM

If you want something a little fast try: 

DateTime systemDate = DateTime.Now;
DateTime compareDate = DateTime.Today.AddHours(11D);

// less than
if (compareDate < systemDate)
    Console.WriteLine("Less Than");

// equal to
if (compareDate == systemDate)
    Console.WriteLine("Equal To");

// greater than
if (compareDate > systemDate)
    Console.WriteLine("Greater Than");

// basically you can compare it in all the normal ways
// using !=, ==, <, >, <=, >=, etc

// or if you just want a straight number result inorder to sort
// you can use
int compareValue = compareDate.Compare(systemDate);
int compareValue2 = DateTime.Compare(systemDate, compareDate);

 So I hope this helps you out.  But remember it is always better to use a lighter weight process.  The Convert or Parse process is pretty heavy because it has to take the string break it apart compare it against a whole set of rules and then based on the rules it has to change them to the correct object.  Or with what I did above you are simply adding two decimal values together which is a much faster process.


Thursday, April 5, 2007 3:20 AM

 It's Windows Coding Convert According to your need,Put within the timer1_Tick event and keep interval = 1,

if(DateTime.Now.TimeOfDay == System.TimeSpan.Parse("00:09:00"))
{
     MessageBox.Show(DateTime.Now.ToString());                           
}

 


Thursday, April 5, 2007 7:58 AM

Here is the code:

DateTime dt11AM = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 11, 0, 0);
if (DateTime.Now < dt11AM)
    Console.WriteLine("It's not 11 AM yet");
else
    Console.WriteLine("It's past 11 AM");
Hope it helps :) 
more tips: http://www.mycsharpcorner.com

Thursday, April 5, 2007 8:33 AM

So many ways to do one simple thing... Not all of them correct though ;-) Here's another one:

 if (System.DateTime.Now.Hour >= 11)
{
    // It's on or after 11 AM!
}

 


Thursday, August 2, 2007 5:36 AM

DateTime t1 = Convert.ToDateTime(DateTime.Now

DateTime t2 = Convert.ToDateTime(("11:00 AM"));

if (t1.TimeOfDay.Ticks > t2.TimeOfDay.Ticks)

{

MessageBox.Show("Time t1 is greater than time t2.");

}


Thursday, August 2, 2007 5:36 AM

DateTime t1 = Convert.ToDateTime(DateTime.Now);

DateTime t2 = Convert.ToDateTime("11:00 AM");

if (t1.TimeOfDay.Ticks > t2.TimeOfDay.Ticks)

{

MessageBox.Show("Time t1 is greater than time t2.");

}