|
-
Jul 20th, 2009, 01:42 AM
#1
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 
-
Jul 20th, 2009, 01:45 AM
#2
Fanatic Member
Re: comparing time
 Originally Posted by motil
Hello,
What is the correct way to compare time in this format "09:30:54" ?
You can use timespan method.
-
Jul 20th, 2009, 01:52 AM
#3
New Member
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.
-
Jul 20th, 2009, 01:54 AM
#4
Hyperactive Member
Re: comparing time
 Originally Posted by motil
What is the correct way to compare time in this format "09:30:54" ?
just create a 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
-
Jul 20th, 2009, 02:08 AM
#5
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 
-
Jul 20th, 2009, 02:11 AM
#6
New Member
Re: comparing time
The time of day is done with
Code:
DateAndTime.TimeOfDay
What are you trying to compare that to?
-
Jul 20th, 2009, 02:24 AM
#7
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 
-
Jul 20th, 2009, 02:37 AM
#8
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?
-
Jul 20th, 2009, 02:40 AM
#9
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 
-
Jul 20th, 2009, 02:50 AM
#10
Re: comparing time
 Originally Posted by motil
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:
Private nextAlarmTime As Date
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Me.nextAlarmTime = Date.Today.Add(My.Settings.AlarmTime)
If Me.nextAlarmTime < Date.Now Then
'The alarm time has passed today so set it for tomorrow.
Me.nextAlarmTime = Me.nextAlarmTime.AddDays(1)
End If
'Start ticking.
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, _
ByVal e As EventArgs) Handles Timer1.Tick
If Me.nextAlarmTime < Date.Now Then
'The alarm time has passed since the last tick.
Me.nextAlarmTime = Me.nextAlarmTime.AddDays(1)
MessageBox.Show("Wake up!")
End If
End Sub
-
Jul 20th, 2009, 02:52 AM
#11
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 
-
Jul 20th, 2009, 02:57 AM
#12
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.
-
Jul 20th, 2009, 03:04 AM
#13
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 
-
Jul 20th, 2009, 05:42 AM
#14
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.
-
Jul 20th, 2009, 05:58 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|