Results 1 to 5 of 5

Thread: [RESOLVED] Calculating 2 times

  1. #1

    Thread Starter
    Member ukjock's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    59

    Resolved [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:
    1. Plushour = DateTime.Now.AddHours(1)
    2.         Dim span As TimeSpan = Plushour.Subtract(DateTime.Now)
    3.         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

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. 'timespan representing the next full hour past the current time
    2.         Dim TargetTime As New TimeSpan(Date.Now.Hour + 1, 0, 0)
    3.         'timespan of the current time
    4.         Dim CurrentTime As New TimeSpan(Date.Now.Hour, Date.Now.Minute, Date.Now.Second)
    5.         'subtracts current time from target time, displays result (time until the next hour)
    6.         Dim ResultTime As TimeSpan = TargetTime.Subtract(CurrentTime)
    7.         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.

  3. #3

    Thread Starter
    Member ukjock's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    59

    Re: Calculating 2 times

    Wow superb, thank you so much.

    That worked a treat!

    kind regards

    chris

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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"

  5. #5

    Thread Starter
    Member ukjock's Avatar
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    59

    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
  •  



Click Here to Expand Forum to Full Width