Solved

Converting Unix style timestamp to a Date in VB6.0

Posted on 2007-11-20
8
678 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 63 total points
ID: 20322096
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 62 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

840 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