Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

Converting Unix style timestamp to a Date in VB6.0

Hi:

I need to be able to convert a Unix style time stamp (ms since 1/1/1970) to a date in vb 6.0.

Dim dblMilliseconds as double
dblMilliseconds = "some timestamp value"

I've got a client waiting on this, so I have to resolve it quickly.

Thanks,
JohnB
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The standard Unix timestamp is seconds since 1/1/1970.  For milliseconds, it would take a minor adjustment:

Option Explicit

Private Sub Command1_Click()
    MsgBox DateFromUnix(1195584810000#)
End Sub

Private Function DateFromUnix(dblTimestamp As Double) As Date
    DateFromUnix = DateAdd("s", dblTimestamp / 1000, #1/1/1970#)

End Function
Avatar of cup
cup

Windows timestamps are also from 1/1/1970.  Time is stored as a float.  Just multiply by 86400000.  The time now would be


cdbl(now) * 86400000
>Windows timestamps are also from 1/1/1970.  Time is stored as a float.  Just multiply by 86400000.  The time now would be

In VB, the date data type maps internally to a double floating point value.  The days are stored in the integer part and the time of day is stored in the fractional part.

Debug.Print CDbl(Now)

yields:

39406.6760185185

That's the number of days since 12/30/1899
Oops - I was playing with VBScript.   Sorry.
It works the same in VBScript.  
So it should have been

(cdbl(now) - cdbl(#1/1/1970#)) * 86400000