VB.NET - Format DateTime With Milliseconds

Despite what people are saying on the net, you do not need to write your own method to include milliseconds when formatting a DateTime object. To achieve this, you can use the f modifier. For example, if you want to include thousands of a second (i.e. milliseconds), you can write something like this:

Now.ToString(“HH:mm:ss.fff”)

You can set the fractional part to include up to 7 digits:

Now.ToString(“HH:mm:ss.fffffff”)

If you try more, it will raise an exception. Luckily, in basic scenarios, you do not need that.