Convert date to string format

ljs235

Newcomer
Joined
Aug 12, 2003
Messages
6
I am logging user actions throughout my program using datetime in the format hh:mm:ss:ff. I would like to calculate the difference between two times. Here is what I'm using for code:

Dim Time As Date
Time = DateTime.Now
Dim TaskTime As TimeSpan = Time.Subtract(AddMainBTime)
LogWriter.WriteLine("*TaskTime " & (TaskTime.ToString))

This works great, but I cannot figure out how to format the TaskTime to hh:mm:ss:ff. Right now, it writes to the file as hh:mm:ss.fffffff. When I try the following, I get the following error: 'Public Overrides Overloads Function ToString() As String' has no parameters and its return type cannot be indexed.

LogWriter.WriteLine("*TaskTime " & (TaskTime.ToString("hh:mm:ss:ff")))

Any ideas on how I can format my TimeSpan value? Thanks. :)
 
You can break a string down using LEFT and RIGHT facilities, i cant remember the exact context for this, but i think this should give u an idea.

MsgBox(Microsoft.VisualBasic.Left("hello", 3))
MsgBox(Microsoft.VisualBasic.Right("hello", 3))

SHOULD return the value "hel" and "llo", you can use this to break the dates down, and remove the annoying 20 out of 2003, or pretty much anything else - obviously you can also embed them into each other.
 
Thanks so much for your help. Here is how I resolved the situation:

LogWriter.WriteLine("*TaskTime " & (Microsoft.VisualBasic.Left(TaskTime.ToString, 11)))

Thank you!!!
 
You should stay away from Left and Right, in general. The string object supports a Substring method which does everything you need. Try this instead:
Visual Basic:
LogWriter.WriteLine("*TaskTime " & TaskTime.ToString().Substring(11))

-Nerseus
 
Back
Top