Results 1 to 7 of 7

Thread: [RESOLVED] Date and Time Calculation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Resolved [RESOLVED] Date and Time Calculation

    Hi all.

    Can someone please provide code for the following calculations using dates and times. I have hit a brick wall with this .

    if the current hour is less than 21:00 (today) then show the remaining time in label3 as hh:mm:ss remaining. However, if the current hour is greater than 21:00 then show show the remaining time calculated in hh:mm:ss from 21:00 on the next day.

    Any help would be appreciated.
    Regards
    Colin

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date and Time Calculation

    Code:
            Dim whenDT As DateTime = #9:00:00 PM#
            Dim dt As DateTime = DateTime.Now
    
            If dt.TimeOfDay >= whenDT.TimeOfDay Then
    
            Else
    
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Date and Time Calculation

    Quote Originally Posted by dbasnett View Post
    Code:
            Dim whenDT As DateTime = #9:00:00 PM#
            Dim dt As DateTime = DateTime.Now
    
            If dt.TimeOfDay >= whenDT.TimeOfDay Then
    
            Else
    
            End If
    Thanks. Could you provide the code that actually does the calculation. Sorry if my post is a bit misleading, I tried to word it as best I could.

    Regards
    Colin

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date and Time Calculation

    Not sure what calculation you mean, but I'll guess
    Code:
            Dim dt As DateTime = DateTime.Now
            Dim whenDT As DateTime = dt.Date.AddHours(21)
            Dim elaps As New TimeSpan
    
            If dt.TimeOfDay > whenDT.TimeOfDay Then
                whenDT = whenDT.AddDays(1)
            End If
            elaps = whenDT - dt
    
            'read about timespan
            Debug.WriteLine(elaps.ToString)
    Last edited by dbasnett; Jan 12th, 2010 at 09:19 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Date and Time Calculation

    I'll see if I can define the required calculations a bit better.

    If the current system time hours < 21:00 then calculate the time difference between current time and 21:00 (formatted as HH:mm:ss)

    else if the current system time > 21:00 then calculate the time difference between current system and 21:00 tomorrow (again formatted as HH:mm:ss).

    Hope this is a bit clearer. Sorry for the confusion with my original post.

    Regards
    Colin

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Date and Time Calculation

    Quote Originally Posted by wearmc View Post
    I'll see if I can define the required calculations a bit better.

    If the current system time hours < 21:00 then calculate the time difference between current time and 21:00 (formatted as HH:mm:ss)

    else if the current system time > 21:00 then calculate the time difference between current system and 21:00 tomorrow (again formatted as HH:mm:ss).

    Hope this is a bit clearer. Sorry for the confusion with my original post.

    Regards
    Colin
    Except for the formatting, that is what you have. BTW - What about
    current system time = 21:00 ??????

    If the current time is before 9PM on this date then show how long till 9PM this date
    else show how long till 9PM tomorrow
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    115

    Re: Date and Time Calculation

    Sorry, didn't understand your code. I thought it was only accounting for the time difference on the same day. My apologies. Will mark thread as resolved.Thanks for your help.

    Regards
    Colin

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