473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Convert ticks to milliseconds

how to convert DateTime.UtcNow.ticks to Milliseconds
Jul 13 '07 #1
8 88535
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, measured in milliseconds, between the current time...
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, currentTimeMillis, is the difference, measured in...
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 Bitmap(@"\Network\im\ImageWeftIllum.bmp"); bmp2...
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 following manner: System.Threading.Timer m_timer = null;...
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 System.currentTimeMillis() ? Thanks.
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 there a way to determine the real elapsed time? ...
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 the result is: "00:01:40" but the result ist:...
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 available as a property of each datetime, but I...
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 facing right now is that, I'm using the 'time()'...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.