Results 1 to 15 of 15

Thread: comparing time

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    comparing time

    Hello,

    What is the correct way to compare time in this format "09:30:54" ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: comparing time

    Quote Originally Posted by motil View Post
    Hello,

    What is the correct way to compare time in this format "09:30:54" ?
    You can use timespan method.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    New Member icedtrees's Avatar
    Join Date
    Jul 2009
    Posts
    12

    Re: comparing time

    That's a bit vague. Compare time? To what? I'll assume you're comparing two times in that format.

    Code:
    Dim MyTime As DateTime
    MyTime = #3:02:23 AM#
    Dim MyTime2 As DateTime
    MyTime2 = #2:12:43 AM#
    Console.WriteLine(DateTime.Compare(MyTime, MyTime2))
    Basically you can create two variables like that, of type DateTime. Then you can use the Compare(First Time,Second Time) Function, which returns -1 if the first time is earlier, and 1 if the first time is later (than the second time)

    Alternatively, if you want to just use values that you pick at non-runtime, you can just do this:

    Code:
    Console.WriteLine(DateTime.Compare(#23:01:42#,#01:37:59#)
    which would write 1 on the console. You get the point.

  4. #4
    Hyperactive Member koolprasad2003's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    443

    Arrow Re: comparing time

    Quote Originally Posted by motil View Post

    What is the correct way to compare time in this format "09:30:54" ?
    just create a
    Code:
    DateTime
    object and store the value in it and compare it
    MCP, MCTS, Microsoft MVP [Asp.Net/IIS]

    For more .NET development tips visit .NET Tips

    If the post is useful then please Rate it

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    How do I store the current time of day as a DateTime?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6
    New Member icedtrees's Avatar
    Join Date
    Jul 2009
    Posts
    12

    Re: comparing time

    The time of day is done with

    Code:
    DateAndTime.TimeOfDay
    What are you trying to compare that to?

  7. #7

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    I'm sorry, Even I not sure what exactly the correct approach to do this,
    I'm trying to create a timer that will tick in certain time of day something like scheduler, first i tried to do it by set the ticker time when the application load:

    Code:
            Dim timeNow As DateTime = Now.TimeOfDay.ToString()
            Dim NextUpdate As DateTime = "09:05:00"
            ' MessageBox.Show(DateDiff(DateInterval.Second, TimeNow, NextUpdate).ToString())
            Timer1.Interval = (DateDiff(DateInterval.Second, timeNow, NextUpdate).ToString() * 1000)
    But this is good only for the first tick, once the tick is finish i must to re-set it to the same time tomorrow, but the calculation will be wrong this time, any ideas?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: comparing time

    Here's a suggestion: instead of posting a vague question about something you think you might need to do, provide a full and clear description of what you're actually trying to achieve in the first post. Then we can help you with your actual problem. If we know what the actual purpose is then we can suggest the best approach to achieve it.

    Basically, you're saying that you want to create an alarm clock, yes? You want to receive a notification at the same time every day, yes?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    well it's not an alarm clock, but yes i need the ticker to tick and call a function once a day
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: comparing time

    Quote Originally Posted by motil View Post
    well it's not an alarm clock, but yes i need the ticker to tick and call a function once a day
    I meant an alarm clock in terms of the essential functionality. Even if that's not the actual thing you're making, wouldn't it have been good to describe it to start with and say "like an alarm clock"? That would have made it clear to us.

    How about this:
    vb.net Code:
    1. Private nextAlarmTime As Date
    2.  
    3. Private Sub Form1_Load(ByVal sender As Object, _
    4.                        ByVal e As EventArgs) Handles MyBase.Load
    5.     Me.nextAlarmTime = Date.Today.Add(My.Settings.AlarmTime)
    6.  
    7.     If Me.nextAlarmTime < Date.Now Then
    8.         'The alarm time has passed today so set it for tomorrow.
    9.         Me.nextAlarmTime = Me.nextAlarmTime.AddDays(1)
    10.     End If
    11.  
    12.     'Start ticking.
    13.     Me.Timer1.Start()
    14. End Sub
    15.  
    16. Private Sub Timer1_Tick(ByVal sender As Object, _
    17.                         ByVal e As EventArgs) Handles Timer1.Tick
    18.     If Me.nextAlarmTime < Date.Now Then
    19.         'The alarm time has passed since the last tick.
    20.         Me.nextAlarmTime = Me.nextAlarmTime.AddDays(1)
    21.  
    22.         MessageBox.Show("Wake up!")
    23.     End If
    24. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    Thank you very much for the code, i will learn it now.

    Thanks again.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: comparing time

    In future, please provide the FULL description in the first post. It avoids a lot of wasted time for you and for us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    I did, at first i only wanted to know how to compare two time strings, then icedtrees asked me what i want to compare so i went into more details.

    and you also need to understand that some people don't know exactly what they need cause they're new to .NET and others have problem with their english and sometimes you have different new questions as the thread is developing.

    I know it's hard to maintain patient when answering so much people every day as you do jm, maybe you need some vacation :B
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: comparing time

    I understand that people don't always know WHAT they want to begin with, which is EXACTLY why they should take the time to explain WHY they want it. If you don't know what you want then how can we? If we know WHY you want it then we can do a much better job of working out WHAT will achieve it best. If people spend a bit more time on their first post they will save themselves, and the rest of us, a lot of time in the long run. I know people are new to VB and have trouble with English but those are reasons to spend MORE time providing a clear explanation, not less.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: comparing time

    you're wrong in some way, its always good to discuss, even on small things, things that look minor to you are important knowledge to others, as for "wasting your time", this is a forums no one force you to answer, if someone decide to answer its means that he wants to and don't mind to "spend his time" as you call it, your contribute to this forums is huge, but you don't need to feel as u have to answer everyone, i learned few new tricks from this thread and my .NET skills are a bit better because of this thread, so I can't say it was waste of time for me.

    anyway, keep the good work with helping others.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

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