How to transform milliseconds into seconds .?
Asked Answered
B

8

17

I have to transform my Stopwatch "Variable" into seconds ?

Stopwatch czasAlg = new Stopwatch(); 
czasAlg.Start();
//Do semothing
czasAlg.Stop();
Decimal Tn = czasAlg.ElapsedMilliseconds/(decimal)n;
Babara answered 14/11, 2010 at 19:33 Comment(2)
Seriously.. you don't know how to transform milliseconds into seconds? :oEmmy
But... why go through the extra step? :-)Centrality
L
47

Divide by 1000 or use

czasAlg.Elapsed.TotalSeconds
Lachrymal answered 14/11, 2010 at 19:35 Comment(0)
C
27

Without your own constants and magic numbers:

TimeSpan.FromMilliseconds(x).TotalSeconds
Coulomb answered 1/4, 2015 at 12:41 Comment(3)
What is the difference of .Seconds and .TotalSeconds on TimeSpan?Stebbins
.Seconds gets the seconds place of the TimeSpan. So if it was 1 minute and 30 seconds you would get 30 back where .TotalSeconds would return 90.Salita
Use TimeSpan.Duration() to get the absolute value of the timespan. TimeSpan.FromMilliseconds(x).Duration().TotalSecondsMclaren
K
25

Just to be different:

Multiply by 0.001.

Kellerman answered 14/11, 2010 at 19:36 Comment(2)
That'll scare the other developers!Satiety
Hm, evil idea: double.Parse(string.Format("{0}e-3", czasAlg.ElapsedMilliseconds))Lachrymal
H
8

Use

czasAlg.Elapsed.TotalSeconds
Hear answered 14/11, 2010 at 19:35 Comment(0)
E
1

Divide by 1000.

Emmy answered 14/11, 2010 at 19:35 Comment(0)
M
0

Divide by 1000.0?

Mandorla answered 14/11, 2010 at 19:35 Comment(0)
N
0

Instead of using math and multiplying/diving like this: seconds (60) * 1000 = 60000, use TimeSpan instead, it's using bit operations, and due to it has a minimum cost of performance.

Milliseconds to seconds

int sixtyThousandsMillisecondsInSeconds = (int)TimeSpan.FromMilliseconds(60000).TotalSeconds;
// Outputs 60

Seconds to milliseconds

// 1 min (60 seconds) in milliseconds = 60000 (i.e 60 * 1000)
int sixtySecondsInMilliseconds = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;
// Outputs 60000

Using LINQPad

int sixtyThousendsMillisecondsInSeconds = (int)TimeSpan.FromMilliseconds(60000).TotalSeconds;
sixtyThousendsMillisecondsInSeconds.Dump();

int sixtySecondsInMilliseconds = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;
sixtySecondsInMilliseconds.Dump();

Results

60

60000

Naumann answered 28/5, 2023 at 18:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.