469,314 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

Convert ticks to milliseconds

how to convert DateTime.UtcNow.ticks to Milliseconds
Jul 13 '07 #1
8 79675
long ms1 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

Marc
Jul 13 '07 #2
Even better would be to use the constructor of the TimeSpan class which
encapsulates the logic, and takes it out of your hands:

TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks);
double ms = ts.TotalMilliseconds;

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:un**************@TK2MSFTNGP05.phx.gbl...
long ms1 = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

Marc

Jul 13 '07 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Even better would be to use the constructor of the TimeSpan class which
encapsulates the logic, and takes it out of your hands:

TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks);
double ms = ts.TotalMilliseconds;
It encapsulates the logic. But the only case where
it encapsulate better than /TimeSpan.TicksPerMillisecond
is if the correlation between Ticks and Milliseconds are
changed to something non proportional. I very much doubt
that would happen.

Arne
Jul 15 '07 #4
Arne Vajhj wrote:
It encapsulates the logic. But the only case where
it encapsulate better than /TimeSpan.TicksPerMillisecond
is if the correlation between Ticks and Milliseconds are
changed to something non proportional. I very much doubt
that would happen.
You are right only if ticks are not dependent on processor clock frequency.

If they are, do remember that lowering the clock frequency to save power is
becoming widely used in portable devices.

Ebbe
Jul 16 '07 #5
In this context, a tick is defined as 100 nanoseconds. It is not
machine dependent.

I would expect lots of pain if this ever changed...

Marc
Jul 16 '07 #6
Marc Gravell wrote:
In this context, a tick is defined as 100 nanoseconds. It is not
machine dependent.
So the value of 'TimeSpan.TicksPerMillisecond' is always 10 no matter what
system, you are working with? Is it guaranteed that it will be 10 in future
systems?
I would expect lots of pain if this ever changed...
Yes, it could create non-trivial problems :-)

Ebbe
Jul 16 '07 #7
I don't know how to answer that directly, but *at the minimum* I
strongly doubt that it would ever change to be non-proportional wrt
time; anybody with a more formal answer?

Marc
Jul 16 '07 #8
Ebbe Kristensen wrote:
Marc Gravell wrote:
>In this context, a tick is defined as 100 nanoseconds. It is not
machine dependent.

So the value of 'TimeSpan.TicksPerMillisecond' is always 10 no matter what
system, you are working with? Is it guaranteed that it will be 10 in future
systems?
It does not need to be constant over time for the division to be
valid. The division only requires the relationship between ticks
and time to be proportional.

Arne
Jul 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jeroen CEuppens | last post: by
21 posts views Thread by Willie jan | last post: by
1 post views Thread by Benny Schudel | last post: by
3 posts views Thread by Mark | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.