Question Display Date & Time Issue

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
I have a scheduler in my program which can be enabled to kick off the series of events at a later time. When the scheduler is enabled and the program is started I'd like to post 2 things into the log, the amount of time its scheduled to run and the amount of time before the software kicks off.

I had no problems doing this when you were running the software within 24hrs. However, I would have problems when trying to schedule this for over 24 hours in the future (for example, 31 hours). This was my initial code
VB.NET:
        txtLog.AppendText("TOTAL TIME SCHEDULED TO RUN:  " & intervalsked.ToString("hh\:mm\:ss") & vbCrLf)
        txtLog.AppendText("TOTAL TIME BEFORE STARTING:  " & beforestart.ToString("hh\:mm\:ss") & vbCrLf)

If I were to try and schedule my program for 31 hours from now, it would instead come up showing 7 hours..

Is there anyway I can take this and make it display the number of days if it will be over 24hrs?

I also tried using a 24hr clock..
VB.NET:
        txtLog.AppendText("TOTAL TIME SCHEDULED TO RUN:  " & intervalsked.ToString("HH:mm:ss") & vbCrLf)
        txtLog.AppendText("TOTAL TIME BEFORE STARTING:  " & beforestart.ToString("HH:mm:ss") & vbCrLf)

When trying this I get a completely different error: Input string was not in a correct format.

Thanks in advance!
 
The TimeSpan.ToString method will display a maximum of 23 hours and, above that, will display a number of days. If you don't want that then you'll have to construct the output text from parts, e.g.
Dim outputText = String.Format("{0:00}:{1:mm\:ss}", CInt(Math.Floor(myTimeSpan.TotalHours)), myTimeSpan)
 
Hmmm... should have read the whole post. Also, you should have read the documentation for the TimeSpan.ToString method.
Dim outputText = myTimeSpan.ToString("d\.hh\:mm\:ss")

If you don't want zero days displayed for values less than 24 hours:
Dim outputText = myTimeSpan.ToString(If(myTimeSpan.Days > 0, "d\.hh\:mm\:ss", "hh\:mm\:ss"))
 
Back
Top