UNIX Timestamp Visual Basic 6
December 5, 2009| Tweet |
Time() Function
Since I started programming in PHP, after Visual Basic, I started to rely on some of the features available with PHP. One of those was the Time() function.Since I don't know of any function that does this in Visual basic, I made this "wrapper" function to calculate with less code.
While I know this works for Visual Basic 6 Enterprise Edition, I'm not sure about earlier versions of Visual Basic. Please comment with your findings.
I do not believe this works with .NET-based languages (Visual Studio 2003 through 2010).
Option Explicit
Dim DateTime As String
Private Function Time(ByVal DateTime As String)
Time = DateDiff("s", "01/1/1970 12:00:00 AM", DateTime)
End Function
Using this function is relatively simple:
Private Sub Form_Load()
Form1.Caption = Time(Now)
End Sub
"Now" is a Visual Basic Constant that contains a current timestamp (like this: 12/04/2009 11:57:00 AM). This will return the current timestamp as Form1's caption property.
You can also insert any (properly formatted) time into this function and get a result. You may get an error using this function if you don't call it / pass variables correctly. I didn't error-check it so you can debug it easier during development.
Proper formats:
mm/dd/yy
mm/dd/yyyy
mm/dd/yyyy hh:mm:ss ap
Try out your specifc requirements, they may work.mm/dd/yyyy
mm/dd/yyyy hh:mm:ss ap
Thanks for reading, please leave a comment below if you liked this article
AmathurNovember 12th, 2010 3:03 am CSTPrint: 815a2ce614da76552644
Great guns !! It works wonderfully well. Robert Thanks a Lot.
Nathaniel GibsonJanuary 31st, 2011 10:17 am CSTPrint: 815a2ce614da76552644
I modified your code so that it would not use VB6 constants and functions like the function Time which is a VB6 constant for the time of day. Thus, the resulting code is...
Private Function UNIXTime(Optional strDT As String) As Long
If Len(strDT) 0 Then strDT Now
UNIXTime DateDiff("s", "0111970 12:00:00 AM", strDT)
End Function
I also changed it so that the argument is optional and Defaults to Now like a PHP function would... I love the way those programmers and Zend think :)
Private Function UNIXTime(Optional strDT As String) As Long
If Len(strDT) 0 Then strDT Now
UNIXTime DateDiff("s", "0111970 12:00:00 AM", strDT)
End Function
I also changed it so that the argument is optional and Defaults to Now like a PHP function would... I love the way those programmers and Zend think :)
Thanks Nathaniel!
It's been quite some time since I've tinkered with VB, it's nice to see that some people are still interested in the language!
It's been quite some time since I've tinkered with VB, it's nice to see that some people are still interested in the language!

Facebook
LinkedIn