7

I am connecting to an API and one of the parameters is Long (13 digits) to hold the current timestamp in VB.Net which represents milliseconds passed from 0:00:00 01.01.1970 in GMT until the current time.

The format should be like this 1290932238757

I tried this syntex :

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

But the output was :

01/12/2013 02:06:24
Walter
  • 2,540
  • 2
  • 30
  • 38
Dan
  • 343
  • 3
  • 9
  • 21

1 Answers1

11

If I understand correctly, does this work?

Dim milliseconds = CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1))
                                                       .TotalMilliseconds)

I've used DateTime.UtcNow in the example, but you can use DateTime.Now depending on how you plan on using the data. See this thread for more information on the difference.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I can't understand how it works: I think it should be CLng(DateTime.UtcNow.Subtract(New DateTime(1970, 1, 1)).ToUniversalTime.TotalMilliseconds). Anyway, this methods does not return the correct timestamp. With @keyboardP call, it returns the correct timestamp, but how is it possible since it compares UtcNow and 1/1/1970 (which is timezone aware)? – Desmond Jul 15 '14 at 09:08
  • I finally understood. The `new Date(1,1,1970)` creates a date which is timezone unaware. The method .ToUniversalTime shifts it like it was in localTime. The best approach should be to create the date timezone-aware: `New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)` – Desmond Jul 15 '14 at 09:53