Epoch time in VB

Hi gurus,

I need your help to convert date time in vb 6.0  to epoch time in milliseconds.

Epoch time format nnnnnnnnnnnnn = 1202422902164.

Regards
kidmaverickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:

Dim epoch0 As Double
Dim dtDate As Date
Const MillisecsPerDay = 86400 * 1000
Const edate = 1202422902164#
 
epoch0 = DateSerial(1970, 1, 1)
dtDate = epoch0 + edate / MillisecsPerDay
MsgBox "Epoch date " & edate & " = " & dtDate

Open in new window

0
mvidasCommented:
Hi kid,

VB6 time doesn't include milliseconds, so it won't show the exact conversion for you.  But you should be able to do something along the lines of:

 EpochTime = DateDiff("s", DateSerial(1970, 1, 1), VB6Time) * 1000

Matt
0
mvidasCommented:
Sorry Graham, poor form on my part; forgot to refresh!
Matt
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GrahamSkanRetiredCommented:
Wrong way round, isn't it. I'
Dim epoch0 As Double
Dim dtDate As Date
Const MillisecsPerDay = 86400 * 1000
 
Dim edate As Double
epoch0 = DateSerial(1970, 1, 1)
dtDate = Now
edate = (dtDate - epoch0) * MillisecsPerDay
MsgBox "Epoch date " & edate & " = " & dtDate

Open in new window

0
PaulHewsCommented:
What Matt said above about milliseconds is true for VB Time and Now functions.  Also, the Epoch timestamp should be based on UTC, not local time.  This can be done if GetSystemTime is used to obtain the current UTC time with milliseconds:
Private Declare Sub GetSystemTime Lib "kernel32.dll" (ByRef lpSystemTime As SYSTEMTIME)
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
 
 
 
Private Sub Command1_Click()
    Dim st As SYSTEMTIME
    GetSystemTime st
    
   Debug.Print UnixEpochFromSystemTime(st)
 
End Sub
 
Private Function UnixEpochFromSystemTime(systime As SYSTEMTIME) As Double
    Dim dt As Date
    
    dt = DateSerial(systime.wYear, systime.wMonth, systime.wDay) + TimeSerial(systime.wHour, systime.wMinute, systime.wSecond)
    UnixEpochFromSystemTime = DateDiff("s", #1/1/1970#, dt) * 1000
    UnixEpochFromSystemTime = UnixEpochFromSystemTime + systime.wMilliseconds
    
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kidmaverickAuthor Commented:
Thank for all.

Amazing and elegant solution PaulHews.

Regards
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.