Get Ticks per second and convert to String value?
Asked Answered
M

4

9

How do I get number of ticks per second of DateTime.UtcNow and convert it to a String value?

BAD QUESTION: try again Get ten millionths of a second

Multiphase answered 26/6, 2010 at 12:7 Comment(3)
var stringValue = "10000000";Vann
BAD QUESTION: try again #3124394 :(Multiphase
Don't repost the same question, add information instead.Hosanna
E
23

A particular value of DateTime doesn't have a "ticks per second" associated with it; ticks are ticks no matter which DateTime they're in. Ticks are 100 nanoseconds long, so there are 10,000,000 of them per second.

Now to get that as a string is as simple as the string literal "10000000"... although in general you would obtain a number and just call ToString() on it. For instance, you could use:

string ticksPerSecond = TimeSpan.TicksPerSecond.ToString();

Your question is a slightly odd one, so I wonder whether we're missing something... could you edit the question with more details about what you're trying to do. For example, are you trying to determine the number of ticks within the particular second of a particular DateTime? That's most easily done as:

long ticks = dt.Ticks % TimeSpan.TicksPerSecond;
Embroidery answered 26/6, 2010 at 12:10 Comment(0)
L
2

I think you may want TimeSpan.TicksPerSecond.

Console.WriteLine("tps = {0}", TimeSpan.TicksPerSecond.ToString());
Liaoyang answered 26/6, 2010 at 12:11 Comment(0)
K
2

You find the ticks per second as a constant on TimeSpan:

TimeSpan.TicksPerSecond

Not sure what you are trying to do though...

(DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond).ToString() // Total number of seconds...
Kinin answered 26/6, 2010 at 12:12 Comment(0)
H
2

The number of ticks per second in a DateTime value is always 10000000. One tick is 100 nanoseconds.

So, if you want to convert that to a string:

10000000.ToString()
Hosanna answered 26/6, 2010 at 12:13 Comment(1)
@simendsjo: Yes, I just wanted to point out that the value never changes. You should avoid using the value altogether, if you want a ticks value as seconds, create a DateTime or TimeSpan value from it and use it's methods to get it as seconds.Hosanna

© 2022 - 2024 — McMap. All rights reserved.