|
-
Feb 28th, 2006, 06:12 AM
#1
Thread Starter
Member
[RESOLVED] Calculating 2 times
I want to calculate the difference from now till the top of the next hour i.e.
12:00:00 pm
minus
11:10:23 am
I have tried the following:
VB Code:
Plushour = DateTime.Now.AddHours(1)
Dim span As TimeSpan = Plushour.Subtract(DateTime.Now)
lbltime_countdown.Text = Format(span.Hours, "00") & ":" & Format(span.Minutes, "00") & ":" & Format(span.Seconds, "00")
Unfortunately the plus hour, adds an hour to the current time so my response is always -01:00:00 is there anyway to set it so that I can simply add an hour to the current time and keep the minutes and seconds to 00??
Thanks in advance
Kind Regards
Chris
-
Feb 28th, 2006, 06:33 AM
#2
Re: Calculating 2 times
You need to subtract the current time from the target time... below is an example that gets the next full hour after the current time, then displays how many minutes and seconds are left over until that time hits. It uses two timespans, the first gets the current hour and adds 1, the second is a timespan of the current hours, minutes, and seconds. Then the resulting timepan is the difference when subtracting the two....
VB Code:
'timespan representing the next full hour past the current time
Dim TargetTime As New TimeSpan(Date.Now.Hour + 1, 0, 0)
'timespan of the current time
Dim CurrentTime As New TimeSpan(Date.Now.Hour, Date.Now.Minute, Date.Now.Second)
'subtracts current time from target time, displays result (time until the next hour)
Dim ResultTime As TimeSpan = TargetTime.Subtract(CurrentTime)
MessageBox.Show(ResultTime.Hours & ":" & ResultTime.Minutes & ":" & ResultTime.Seconds)
If you are wanting to set it at a particular time of the day, then modify the first timespan with the hour you want to set it on... ex.. 12 noon would be Timespan(12,0,0), 5pm would be (17,0,0), etc...
Last edited by gigemboy; Feb 28th, 2006 at 06:37 AM.
-
Feb 28th, 2006, 06:40 AM
#3
Thread Starter
Member
Re: Calculating 2 times
Wow superb, thank you so much.
That worked a treat!
kind regards
chris
-
Feb 28th, 2006, 06:45 AM
#4
Re: Calculating 2 times
No prob, welcome to the forum If your threads are resolved, you can go to the top under "thread tools" and mark it as "resolved"
-
Feb 28th, 2006, 06:47 AM
#5
Thread Starter
Member
Re: [RESOLVED] Calculating 2 times
What a cool and handy feature, thanks for that.
c
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
|