Pages

SPONSORED

Epoch Time

Epoch time is the number of seconds elapsed since January 1, 1970. This is also called as UNIX time or POSIX time. It is widely used in UNIX operation system.

Epoch time is 0 at 00:00:00 1/1/1970. This number is simply increments by 1 every second, without bothering about week, month and year. It is stored as 32 bit unsigned integer and hence can cover 136 years totally. This means it reaches its dead end in year 2038. It causes overflow after that particular date. I read on wiki site that the overflow occurs at one second after 03:14:00 1/19/2038.


Why 1/1/1970?

All this research of representing time in UNIX was carried out in early 1970s. They had to set epoch time as some time in the recent past and hence 1/1/1970 00:00:00 was set as epoch time. The number 0 is also called as epoch.

Epoch time to regular time and vice versa

Now that we know we have something called epoch time and why did it come into existence. The following table shows how to calculate epoch time from regular time and vice versa. We also have ways to calculate current time here.

Regular time to epoch

Java
long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000;
VBScript/ASP
DateDiff("s", "01/01/1970 00:00:00", time field)
SQL Server
SELECT DATEDIFF(s, '1970-01-01 00:00:00', time field)



Epoch to regular time

Java
String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
VBScript/ASP
DateAdd("s", epoch, "01/01/1970 00:00:00")
SQL Server
DATEADD(s, epoch, '1970-01-01 00:00:00')



Get current time in epoch
Java
long epoch = System.currentTimeMillis()/1000;
Microsoft .NET C#
epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
VBScript/ASP
DateDiff("s", "01/01/1970 00:00:00", Now())
SQL Server
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
JavaScript
Math.round(new Date().getTime()/1000.0) getTime() returns time in milliseconds.