?
Solved

Converting Unix style timestamp to a Date in VB6.0

Posted on 2007-11-20
8
Medium Priority
?
863 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:jxbma
  • 4
  • 3
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 252 total points
ID: 20322096
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 248 total points
ID: 20322110
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 20322138
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:cup
ID: 20323097
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 20323581
>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
 
LVL 11

Expert Comment

by:cup
ID: 20326075
Oops - I was playing with VBScript.   Sorry.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20328085
It works the same in VBScript.  
0
 
LVL 11

Expert Comment

by:cup
ID: 20331870
So it should have been

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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question