timespan problem

  • Thread starter Thread starter Dirk Reske
  • Start date Start date
D

Dirk Reske

hey,

when I do:

TimeSpan ts = TimeSpan.FromTicks(System.Enviroment.TickCount);

ts.ToString();

I get something like this: 00:00:00.2381474
but I want to see, how many hours, minutes....this are...

what can I do?
 
Dirk Reske said:
when I do:

TimeSpan ts = TimeSpan.FromTicks(System.Enviroment.TickCount);

ts.ToString();

I get something like this: 00:00:00.2381474
but I want to see, how many hours, minutes....this are...

what can I do?

Look at the various properties of TimeSpan such as Hours, Minutes etc.
 
Dirk Reske said:
these are allways 0

In that case your TimeSpan is shorter than a minute - in your example,
for instance, it's only 238 milliseconds long, so yes, the hours and
minutes values *would* be 0.
 
when I do:
TimeSpan ts = TimeSpan.FromTicks(System.Environment.TickCount);

textBox1.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" +
ts.Seconds.ToString();

I get: 0:0:6 my computer is on for more than 15 hours now!!!!!
 
Dirk Reske said:
when I do:
TimeSpan ts = TimeSpan.FromTicks(System.Environment.TickCount);

textBox1.Text = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" +
ts.Seconds.ToString();

I get: 0:0:6 my computer is on for more than 15 hours now!!!!!

The problem is in what is meant by "tick" here. From
System.Environment.TickCount:

<quote>
Property Value
A 32-bit signed integer containing the amount of time in milliseconds
that has passed since the last time the computer was started.
</quote>

Note the "in milliseconds" bit.

Now from TimeSpan.FromTicks:
<quote>
Remarks
This is a convenience method with the same behavior as the TimeSpan
constructor.
</quote>

the constructor in question is TimeSpan(long), which has this
information:

<quote>
Parameters
ticks
A time period expressed in 100-nanosecond units.
</quote>

So Environment.TickCount is giving it in milliseconds, and you're then
constructing a TimeSpan with that number of 100-nanosecond units.
Instead of that, if you use TimeSpan.FromMilliseconds() you'll get a
much more appropriate figure.
 
Back
Top