How do you get the current time in milliseconds (long), in VB.Net?
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.
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()
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
Get the difference between the current time and the time origin, use the TotalMilliseconds
property to get time span as milliseconds, and cast it to long.
DirectCast((Datetime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds, Int64)
For information, my personal case was fixed with another way, without getting the exact same value as a 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)