472,353 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Convert ticks to milliseconds

how to convert DateTime.UtcNow.ticks to Milliseconds
Jul 13 '07 #1
8 86026
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 Vajhøj 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Num | last post by:
Hi all, I have to convert a J2EE date as a long ("Millis") in a .NET date as a long ("Ticks") In Java, currentTimeMillis, is the difference,...
1
by: Num | last post by:
Hi all, Sorry for cross-posting. I have to convert a J2EE date as a long ("Millis") in a .NET date as a long ("Ticks") In Java,...
4
by: Jeroen CEuppens | last post by:
Hi, I want to measure a time (in ticks) between a couple of operations, with these code: long voor = DateTime.Now.TimeOfDay.Ticks; bmp1 = new...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the...
10
by: yaron | last post by:
Hi, I do i get the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. like in java...
21
by: Willie jan | last post by:
place this behind a button that fills a listbox. as you will see the time is now and then 0 or filled in???????????? by hitting the button. is...
1
by: Benny Schudel | last post by:
hello I've tried to convert some milliseconds to a time format. $ms = 100000 // 1min 40sec echo strftime('%H:%M:%S', $ms/1000); i expect...
3
by: Mark | last post by:
I'd like to compare two datetime values in milliseconds. The datetime.compare method appears to show only seconds. Milliseconds of a datetime are...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.