在 VB.Net 中,如何以毫秒(长)为单位获取当前时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4385853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:11:13  来源:igfitidea点击:

How do you get the current time in milliseconds (long), in VB.Net?

vb.nettime

提问by Gnoupi

I'm looking for the equivalent to a Java System.currentTimeMilli(), in VB.NET.

我正在System.currentTimeMilli()VB.NET 中寻找相当于 Java 的东西。

What is the method to call? I know about Datetime.Now, but not about the actual conversion to long milliseconds.

调用的方法是什么?我知道 Datetime.Now,但不知道实际转换为长毫秒。



More details about my specific need:

有关我的特定需求的更多详细信息:

I need to manage a login expiration. So most likely when I log in, I will set a "expiration_time_milli", equal to the current time + the timeout value. Then later, if I want to check if my login is valid, I will check is "expiration_time_milli" is still superior to current time.

我需要管理登录过期。所以很可能在我登录时,我会设置一个“expiration_time_milli”,等于当前时间+超时值。然后,如果我想检查我的登录是否有效,我会检查“expiration_time_milli”是否仍然优于当前时间。

采纳答案by Guffa

Get the difference between the current time and the time origin, use the TotalMillisecondsproperty to get time span as milliseconds, and cast it to long.

获取当前时间与时间原点的差值,使用该TotalMilliseconds属性获取时间跨度为毫秒,并将其转换为long。

DirectCast((Datetime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds, Int64)

回答by jatin

You could use

你可以用

(DateTime.Now-new DateTime(1970,1,1)).TotalMilliseconds

Btw, just guessing by your question what you could be more useful to you might be

顺便说一句,只是通过你的问题猜测你可能对你更有用

DateTime.UtcNow

回答by Gnoupi

For information, my personal case was fixed with another way, without getting the exact same value as a System.currentTimeMilli():

有关信息,我的个人案例是用另一种方式修复的,没有获得与 System.currentTimeMilli() 完全相同的值:

Dim loginExpirationDate As Date

'...

'Checking code:
If (loginExpirationDate < DateTime.Now) Then
    reconnect()
End If

'update the expiration date
loginExpirationDate = DateTime.Now.AddMilliseconds(timeoutMilli)

回答by Jon Skeet

It's somewhat alarming to see all the answers here referring to DateTime.Now, which returns the localtime (as in "in the system default time zone") - whereas System.currentTimeMillis()returns the number of milliseconds since the Unix epoch (1970-01-01T00:00:00Z). All the answers here should probably refer to DateTime.UtcNowinstead.

看到这里引用的所有答案有点令人震惊DateTime.Now,它返回本地时间(如“在系统默认时区中”) - 而System.currentTimeMillis()返回自 Unix 纪元 (1970-01-01T00:00:00Z) 以来的毫秒数)。这里的所有答案可能都应该参考DateTime.UtcNow

Fortunately, somewhat after this question was asked, a much simpler approach has been made available, via DateTimeOffset.ToUnixTimeMilliseconds(). So you just need:

幸运的是,在提出这个问题之后,通过DateTimeOffset.ToUnixTimeMilliseconds(). 所以你只需要:

Dim millis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()

回答by Janak

You may also use this value, if you are looking for just a unique number. DateTime.Now.Ticks

如果您只是在寻找唯一的数字,您也可以使用此值。 DateTime.Now.Ticks

See Details http://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx

查看详细信息 http://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx

回答by www-0av-Com

Put simply DateTime.Now.Ticks / 10000

简单地说 DateTime.Now.Ticks / 10000

The is the most direct answerto the simple question asked, and is a proven direct substitute to Java's System.currentTimeMillis()assuming it is required to measure elapsed time (in that it runs from Jan 1, 0001, whereas Java runs from Jan 1 1970).

这是对所问的简单问题的最直接的回答,并且是 Java 的一个经过验证的直接替代品,System.currentTimeMillis()假设需要测量经过的时间(因为它从 0001 年 1 月 1 日开始运行,而 Java 从 1970 年 1 月 1 日开始运行)。

Notes:

笔记:

  • DateTime.Now.Ticks is 10,000 ticks per millisec, but tests show it has a resolution of approx 1ms anyway.
  • Nb: DateTime.Now.Millisecondis just the millisecs since last second rollover, so is not useful in most timing measurements.
  • DateTime.Now.Ticks 是每毫秒 10,000 次滴答,但测试表明它无论如何都有大约 1 毫秒的分辨率。
  • Nb:DateTime.Now.Millisecond只是自上一秒翻转以来的毫秒数,因此在大多数计时测量中没有用。

回答by halfevil

    Dim myTime As DateTime = DateTime.Now
    MessageBox.Show(myTime.Millisecond)