שתף באמצעות


How to round Milliseconds digits in Datetime.now from 7 digits to 3?

Question

Thursday, April 30, 2009 2:05 PM

Hi,

Dim Timing1, Timing2 As DateTime

        Timing1 = DateTime.Now

 

        ‘MyCode

       

        Timing2 = DateTime.Now

       

        TextBox1.Text = TextBox1.Text + "Printing  " & (Timing2 – Timing1).ToString

 

I need to test the time spend for a procedure. The result in this format is:
00:00:05.45123544

I want to reduce the number of Millisecond digits to a certain number. For exmaple (to four digits):

00:00:05.4512
Please note that I am using visual basic 2008 express edition.

Kind Regards

K Fendi - UK

All replies (13)

Thursday, April 30, 2009 3:25 PM ✅Answered | 1 vote

I would do something like this:

 

Dim Timing1 As DateTime, Timing2 As DateTime
Dim Elapsed_ms As TimeSpan

Timing1 = DateTime.Now 
'MyCode
Timing2 = DateTime.Now Elapsed_ms.Milliseconds

Elapsed_ms = Timing2 - Timing1

TextBox1.Text = Elapsed_ms.Seconds & "." & Elapsed_ms.MiliSeconds


Thursday, April 30, 2009 4:06 PM ✅Answered | 1 vote

My question was whether you are using my method or the method devised by pro2c.

Replace this statement
t.hours & ":" & t.minutes & ":" & t.seconds & "." & math.round(t.milliseconds, 4)

with this one

t.hours.tostring.padleft(2, "0") & ":" & t.minutes.tostring.padleft(2, "0") & ":" & t.seconds.tostring.padleft(2, "0") & "." & math.round(t.milliseconds, 4) ___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 2:34 PM | 1 vote

Use Match.Round(Value, 4)

Value of the milliseconds could be obtained by either splitting the string result on . and using the second element of the array or using the (Timing2-Timing1).Milliseconds ___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 3:16 PM

HI,

Thank you for your response.
I think what mean by Match.Round is the Math.Round.
Having saying that, How can use it?
is it like this?
TextBox1.Text = TextBox1.Text + "Printing  " & Match.Round(Val(Timing2 – Timing1),4).ToString

But this did not wrok with me!!
Please advice.

Many ThanksK Fendi - UK


Thursday, April 30, 2009 3:23 PM | 1 vote

Yes i meant math.round. That's a typo.

Timing2-Timing1 yields a TimeSpan class. You need to use the timespan.milliseconds property to get the milliseconds part only. Then you need to join these all together. For example

dim t as timespan = timing2 - timing1
dim str as string = t.hours & ":" & t.minutes & ":" & t.seconds & "." & math.round(t.milliseconds, 4)
___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 3:34 PM

@pro2c,
The actual question is to round the milliseconds! ___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 3:36 PM

The TimeSpan miliseconds does not produce 7 digits! only 4C64


Thursday, April 30, 2009 3:38 PM

Why is it producing 7 digits in this statement then?
(Timing2 – Timing1).ToString
(Timing2 - Timing1) returns a TimeSpan object.
___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 3:39 PM

True, that is why I modified his code, so it does notC64


Thursday, April 30, 2009 3:44 PM

Hi,

That was very helpful.
This gave me this format:
0:0:0.0000

How do I convert it to this format:

00:00:00.0000

Thanks again.K Fendi - UK


Thursday, April 30, 2009 3:45 PM

Which method are you using? ___________________________________________ If this post is useful, mark it as answer.


Thursday, April 30, 2009 4:02 PM

HI again,

Wuold you please let me know what do you mean by your question?
I am using this (if this is what you mean)
 t.hours & ":" & t.minutes & ":" & t.seconds & "." & math.round(t.milliseconds, 4)

but I may use the other in the future.

Thank you.K Fendi - UK


Thursday, April 30, 2009 4:09 PM

HI,

Thank you. I got it.

Many Thanks.K Fendi - UK