Results 1 to 16 of 16

Thread: Compare Time in text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Singapore
    Posts
    93

    Compare Time in text

    How do I compare Timings like "06:00 AM is earlier than 05:30 AM"??
    An elephant learning VB..

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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");
    		}
    
    	}
    }

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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;
    		}
    
    	}
    }

  4. #4
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Dang! You beat me to it you weenie! I'll post this example anyway.


    Furry
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  5. #5
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Oops forgot the attachment. I hate when I do that...
    Attached Files Attached Files
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    lol.....well, if you weren't so fat and furry you might have beet me 2 it...

  7. #7
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    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.
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    He stated that he wants to compare times. All you're example does is perform arithmetic operations. Big difference...

  9. #9
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    But that IS coparing times, isn't it? It's comparing the current time to the entered time!

    ...For some reason that embarrassed smiley doesn't really look all that embarrased to me...
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    For one, your example doesn't take into consideration AM or PM.

  11. #11
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Does yours? I apologize for my ignorance, but I don't really quite understand your code...
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  13. #13
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Oh. I didn't know that. Thanks for the info.
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe the VB.NEt version would help:

    VB Code:
    1. Dim tOne As DateTime = DateTime.Now
    2.         Dim tTwo As DateTime = DateTime.Now.AddHours(2)
    3.  
    4.         Select Case tOne.CompareTo(tTwo)
    5.         Case -1
    6.                 MsgBox("tOne is less than tTwo")
    7.             Case 0
    8.                 MsgBox("tOne is equal to tTwo")
    9.             Case 1
    10.                 MsgBox("tOne is greater than tTwo")
    11.         End Select

  15. #15
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    That's my fault. I keep forgetting I'm posting C# code instead of vb.net.

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width