PDA

Click to See Complete Forum and Search --> : Compare Time in text


TaNz
Oct 31st, 2002, 07:33 PM
How do I compare Timings like "06:00 AM is earlier than 05:30 AM"??:confused:

Lethal
Oct 31st, 2002, 07:52 PM
Here is one way:


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");
}

}
}

Lethal
Oct 31st, 2002, 07:56 PM
Or..


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;
}

}
}

Fat_N_Furry
Oct 31st, 2002, 09:32 PM
Dang! You beat me to it you weenie! :D I'll post this example anyway.


Furry

Fat_N_Furry
Oct 31st, 2002, 09:32 PM
Oops forgot the attachment. :rolleyes: I hate when I do that...

Lethal
Oct 31st, 2002, 09:46 PM
lol.....well, if you weren't so fat and furry you might have beet me 2 it...;)

Fat_N_Furry
Oct 31st, 2002, 09:49 PM
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

Lethal
Oct 31st, 2002, 09:58 PM
He stated that he wants to compare times. All you're example does is perform arithmetic operations. ;) Big difference...

Fat_N_Furry
Nov 1st, 2002, 10:26 AM
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...

Lethal
Nov 1st, 2002, 11:57 AM
For one, your example doesn't take into consideration AM or PM.

Fat_N_Furry
Nov 1st, 2002, 12:56 PM
Does yours? I apologize for my ignorance, but I don't really quite understand your code... :confused:

Lethal
Nov 1st, 2002, 01:05 PM
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.

Fat_N_Furry
Nov 1st, 2002, 01:07 PM
Oh. I didn't know that. Thanks for the info. :)

Edneeis
Nov 1st, 2002, 01:12 PM
Maybe the VB.NEt version would help:


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

Lethal
Nov 1st, 2002, 01:24 PM
That's my fault. I keep forgetting I'm posting C# code instead of vb.net.

Edneeis
Nov 1st, 2002, 02:05 PM
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. ;)