在 VB.Net 中,如何以毫秒(长)为单位获取当前时间? [英] How do you get the current time in milliseconds (long), in VB.Net?

查看:103
本文介绍了在 VB.Net 中,如何以毫秒(长)为单位获取当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 VB.NET 中寻找与 Java System.currentTimeMilli() 等效的东西.

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

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

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

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

More details about my specific need:

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

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.

推荐答案

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

It's somewhat alarming to see all the answers here referring to DateTime.Now, which returns the local time (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.UtcNow instead.

幸运的是,在提出这个问题之后,通过 DateTimeOffset.ToUnixTimeMilliseconds() 提供了一种更简单的方法.所以你只需要:

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

Dim millis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()

这篇关于在 VB.Net 中,如何以毫秒(长)为单位获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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