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

编程入门 行业动态 更新时间:2024-10-18 06:02:16
本文介绍了在 VB.Net 中,如何以毫秒(长)为单位获取当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 13:08:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单位   时间   VB   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!