获取每秒刻度数并转换为字符串值?

发布于 2024-09-07 07:20:32 字数 161 浏览 2 评论 0原文

如何获取 DateTime.UtcNow 每秒的刻度数并将其转换为字符串值?

糟糕的问题:再试一次获取百万分之十秒

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏闹i 2024-09-14 07:20:32

DateTime 的特定没有与之关联的“每秒滴答数”;无论处于哪个 DateTime 中,刻度都是刻度。刻度的长度为 100 纳秒,因此每秒有 10,000,000 个刻度。

现在将其作为字符串获取就像字符串文字“10000000”一样简单......尽管通常您会获得一个数字并只需对其调用 ToString() 即可。例如,您可以使用:

string ticksPerSecond = TimeSpan.TicksPerSecond.ToString();

您的问题有点奇怪,所以我想知道我们是否遗漏了某些内容...您可以编辑问题,并提供有关您想要做什么的更多详细信息吗?例如,您是否试图确定特定DateTime的特定秒内的刻度数?最容易做到的是:

long ticks = dt.Ticks % TimeSpan.TicksPerSecond;

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;
焚却相思 2024-09-14 07:20:32

我认为您可能需要 TimeSpan.TicksPerSecond

Console.WriteLine("tps = {0}", TimeSpan.TicksPerSecond.ToString());

I think you may want TimeSpan.TicksPerSecond.

Console.WriteLine("tps = {0}", TimeSpan.TicksPerSecond.ToString());
半城柳色半声笛 2024-09-14 07:20:32

您发现每秒的滴答声在 TimeSpan 上是一个常量:

TimeSpan.TicksPerSecond

但不确定您要做什么......

(DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond).ToString() // Total number of seconds...

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...
り繁华旳梦境 2024-09-14 07:20:32

DateTime 值中每秒的刻度数始终为 10000000。一个刻度为 100 纳秒。

因此,如果您想将其转换为字符串:

10000000.ToString()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文