Results 1 to 5 of 5

Thread: Current System Time - Subtract 00:01:58 (HH:mm:ss)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    17

    Question Current System Time - Subtract 00:01:58 (HH:mm:ss)

    Hi All,

    Hopefully this will be quite simple, but I can't figure this one out myself and I can't find what to search for to help myself.

    What I need to do is take the current time for example 05:16:58 and subtract 00:01:58 (1 minute and 58 seconds) from it and then keep it on a timer to tick like a clock (in a textbox), so in this case it would be 05:15:00 and keep ticking up from there. I've tried a few different ways of doing this by seperating the hours/minutes/seconds in different textboxes, then subtracting what I needed and then piecing them back together in another textbox. This only caused issues by sometimes giving me minus figures and it obviously didn't behave like a clock.

    I need this to happen automatically when the form loads, which means I couldn't use a variable for 'As New DateTime' as I'd have to set the difference manually every time it opened.

    Reason for this - I'm making a clock which shows our Mainframe time which is slightly behind our Windows PC time and I just want to show the current time of the Mainframe in a textbox. Doesn't have to be exact, just a lot closer than 1 minute 58 seconds out!

    Any ways of doing this would be awesome!

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    17

    Re: Current System Time - Subtract 00:01:58 (HH:mm:ss)

    Ok, I've found that I can use the 'AddSeconds' with a minus figure, I just need to work out how to do this for minutes and seconds whilst still showing the hours.

    Code:
    Dim newDate As DateTime = Now.AddSeconds(-58)

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: Current System Time - Subtract 00:01:58 (HH:mm:ss)

    Your description didn't really make sense until I read your second last paragraph. You could have explained it much more simply in the first place, e.g. "I want to diaplay a clock where the time displayed is one minute and 58 seconds before the current system time". That's actually quite easy to do. If you wanted a clock that showed the actual time then you'd use a Timer with an appropriate Interval, e.g. 1000 ms, and then convert Date.Now to String and display the result. For your time correction, you simply subtract the appropriate amount of time from Date.Now, which you could do in multiple ways, e.g.
    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.Label1.Text = Date.Now.AddSeconds(-118).ToShortDateString()
    End Sub
    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.Label1.Text = Date.Now.Subtract(New TimeSpan(0, 1, 58)).ToShortDateString()
    End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    17

    Re: Current System Time - Subtract 00:01:58 (HH:mm:ss)

    Apologies for the long winded description. With what you've given me I'm almost there.

    I changed the last part of the code to display the time (ToShortTimeString), how do I show the seconds also (HH:mm:ss)? I don't need to display the date.

    Code:
    Me.Label1.Text = Date.Now.Subtract(New TimeSpan(0, 1, 58)).ToShortTimeString()
    Thank you very much. Great to learn something new.
    Last edited by John757; Apr 16th, 2015 at 12:58 AM.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: Current System Time - Subtract 00:01:58 (HH:mm:ss)

    Oops! That should have been `ToShortTimeString` rather than `ToShortDateString`. That will use the system default short time format to display the specified time. You can also call `ToLongTimeString` to use the system default long time format or `ToString` and provide your own standard or custom time format specifier.

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