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
LVL 1
jxbmaSoftware ConsultantAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
0
 
PaulHewsConnect With a Mentor Commented:
Option Explicit

Private Sub Command1_Click()
    MsgBox DateFromUnix(1195584810)
End Sub

Private Function DateFromUnix(lngTimestamp As Long) As Date
    DateFromUnix = DateAdd("s", lngTimestamp, #1/1/1970#)

End Function
0
 
PaulHewsCommented:
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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
cupCommented:
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
0
 
PaulHewsCommented:
>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
0
 
cupCommented:
Oops - I was playing with VBScript.   Sorry.
0
 
PaulHewsCommented:
It works the same in VBScript.  
0
 
cupCommented:
So it should have been

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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.