How do I compare Timings like "06:00 AM is earlier than 05:30 AM"??:confused:
Printable View
How do I compare Timings like "06:00 AM is earlier than 05:30 AM"??:confused:
Here is one way:
Code:using System;
public class TimeExample
{
public static void Main()
{
DateTime tOne = DateTime.Now;
DateTime tTwo = DateTime.Now.AddHours(2);
if (tOne > tTwo)
{
Console.WriteLine("tOne is greater than tTwo");
}
else
{
Console.WriteLine("tTwo is greater than tOne");
}
}
}
Or..
Code:using System;
public class TimeExample
{
public static void Main()
{
DateTime tOne = DateTime.Now;
DateTime tTwo = DateTime.Now.AddHours(2);
switch (tOne.CompareTo(tTwo))
{
case -1:
Console.WriteLine("tOne is less than tTwo");
break;
case 0:
Console.WriteLine("tOne is equal to tTwo");
break;
case 1:
Console.WriteLine("tOne is greater than tTwo");
break;
}
}
}
Dang! You beat me to it you weenie! :D I'll post this example anyway.
Furry
Oops forgot the attachment. :rolleyes: I hate when I do that...
lol.....well, if you weren't so fat and furry you might have beet me 2 it...;)
LOL actually, I'm really not Fat_N_Furry. My cat is. He weighs 20 pounds, for crying out loud! I'm Skinny_N_Hairless. LOL
I think my example's better, though. :p
He stated that he wants to compare times. All you're example does is perform arithmetic operations. ;) Big difference...
But that IS coparing times, isn't it? It's comparing the current time to the entered time! :o
...For some reason that embarrassed smiley doesn't really look all that embarrased to me...
For one, your example doesn't take into consideration AM or PM.
Does yours? I apologize for my ignorance, but I don't really quite understand your code... :confused:
Yes, b/c we are using DateTime objects, not just integers. These are two seperate datatypes. They are optimized to work with real date and time data.
Oh. I didn't know that. Thanks for the info. :)
Maybe the VB.NEt version would help:
VB Code:
Dim tOne As DateTime = DateTime.Now Dim tTwo As DateTime = DateTime.Now.AddHours(2) Select Case tOne.CompareTo(tTwo) Case -1 MsgBox("tOne is less than tTwo") Case 0 MsgBox("tOne is equal to tTwo") Case 1 MsgBox("tOne is greater than tTwo") End Select
That's my fault. I keep forgetting I'm posting C# code instead of vb.net.
Its my personal opinion that everyone should at know how to translate back and forth (C# <-> V.NET) so maybe that will encourage people. Of course I guess I messed it up then, but hey. ;)