Unix time (aka. POSIX time) is the number of seconds elasped since midnight UTC January 1, 1970 (not counting leap seconds) and is widely used in not only Unix-like operating systems but in many other computer systems as well. I found myself needing to find the UNIX timestamp very often (every time I packaged something for Installer.app) and I was using obnoxious PHP scripts I had found online. I wanted to make myself a small application that would determine the current UNIX timestamp from my current system time.
There are tons of ways to do this but I believe I found the most efficient… It consists of only one line of code:
DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds
If that beautiful little line of code is thrown into a function like so:
Function UnixTimeStamp() As Integer
Return DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds
End Function
It provides a very easy way to call UnixTimeStamp() from anywhere in your program to receive the current UNIX timestamp from your system time.
It’s worth mentioning that this little function does not take timezones into account so you will have to add code to do so yourself. For example, if you are in Eastern Standard Time (UTC-5), you could modify the function like so:
Function UnixTimeStamp() As Integer
Return DateTime.UtcNow.Subtract(#1/1/1970#).TotalSeconds - 18000
End Function
You don’t really need an example application for this snippet of code but, nonetheless, I’ve included the source (in both vb.net and .txt forms) and a standalone .exe for a simple application that, when launched, displays the current UNIX timestamp and a refresh button to update the timestamp.
Download VB.NET 2008 Source Code