自C#中的Unix纪元以来如何获得毫秒数? [英] how to get milliSeconds since the Unix epoch in C# ?

查看:210
本文介绍了自C#中的Unix纪元以来如何获得毫秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我得到的差异,以毫秒为单位,在当前时间和1970年1月1日午夜时间之间。



类似于java System.currentTimeMillis()?


谢谢。

解决方案
"亚龙" <亚*** @ discussions.microsoft.com>在消息中写道

news:38 ********************************** @ microsof t.com ...



我能得到与当前时间和1970年1月1日午夜之间的差异,以毫秒为单位。 UTC。
类似于java System.currentTimeMillis()?




你可以使用DateTime.Now.Ticks。


它将为您提供从1月1日凌晨00:00起的100纳秒的数量,

0001,但您应该能够将其转换为您需要的数量。

问候

Anders


Anders K. Olsen写道:

我得到的差异,以毫秒为单位,以当前时间和1970年1月1日午夜时间为准。
在java System.currentTimeMillis()中?



你可以使用DateTime.Now.Ticks。

它会给你自上午12:00起100纳秒的数量, 1月1日,
0001,但你应该可以将它转换成你需要的。




它可能更容易使用DateTime.Now-新的DateTime(1970,1,1)和

然后使用返回的TimeSpan的TotalMilliseconds属性。


您可能还想考虑使用DateTime.UtcNow而不是

DateTime.Now - 这取决于具体的应用。


Jon


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

Anders K. Olsen写道:

>我能得到差异,以毫秒为单位,介于
>之间。当前时间和1970年1月1日午夜UTC。
>比如java System.currentTimeMillis()?



你可以使用DateTime.Now.Ticks。

它会给你12个以来100纳秒的数量:上午00:00
1,
0001,但您应该可以将其转换为您需要的。



使用DateTime可能更容易.Now-new DateTime(1970,1,1)和
然后使用返回的TimeSpan的TotalMilliseconds属性。

您可能还想考虑使用DateTime.UtcNow而不是
DateTime.Now - 这取决于具体的应用程序。




当然,你是对的。


我''我本身就是一名Java程序员,我发现我使用System.currenTimeMillis()的大部分地方都是计算时间跨度的。对于

这些情况我并不真正关心确切的数字,因为我只需要

的差异。为此,我发现DateTime.Now.Ticks很好地工作了很多。


我刚才意识到,我实际上可能会遇到问题,如果我在夏令时间变换之前和之后尝试比较

...我最好使用UtcNow

而不仅仅是现在将来。


问候

Anders


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.

解决方案

"yaron" <ya***@discussions.microsoft.com> wrote in message
news:38**********************************@microsof t.com...

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() ?



You can use DateTime.Now.Ticks.

It will give you the number of 100 nanoseconds since 12:00 A.M., January 1,
0001, but you should be able to convert it to what you need.

Regards
Anders


Anders K. Olsen wrote:

I do i get the difference, measured in milliseconds, between
the current time and midnight, January 1, 1970 UTC.
like in java System.currentTimeMillis() ?



You can use DateTime.Now.Ticks.

It will give you the number of 100 nanoseconds since 12:00 A.M., January 1,
0001, but you should be able to convert it to what you need.



It''s probably easier to use DateTime.Now-new DateTime(1970,1,1) and
then use the TotalMilliseconds property of the returned TimeSpan.

You might also want to consider using DateTime.UtcNow rather than
DateTime.Now - it depends on the exact application.

Jon


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Anders K. Olsen wrote:

> I do i get the difference, measured in milliseconds, between
> the current time and midnight, January 1, 1970 UTC.
> like in java System.currentTimeMillis() ?



You can use DateTime.Now.Ticks.

It will give you the number of 100 nanoseconds since 12:00 A.M., January
1,
0001, but you should be able to convert it to what you need.



It''s probably easier to use DateTime.Now-new DateTime(1970,1,1) and
then use the TotalMilliseconds property of the returned TimeSpan.

You might also want to consider using DateTime.UtcNow rather than
DateTime.Now - it depends on the exact application.



Of course, you are right.

I''m originally a Java programmer myself, and I have found that most of the
places where I use System.currenTimeMillis() was to calculate timespans. For
these cases I didn''t realy care about the exact number, since I only needed
the difference. For this purpose, I have found that DateTime.Now.Ticks work
very well.

I just realized, that I may actually run into a problem, if I try to compare
times before and after a daylight savings time shift... I better use UtcNow
instead of just Now in the future.

Regards
Anders


这篇关于自C#中的Unix纪元以来如何获得毫秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆