Epoch Converter Functions

Index

Find more epoch conversion routines (in other programming languages) on the homepage.

VBScript / ASP routines

Warning! You need to correct the time zone for these examples to work.

A manual quickfix is to either:

  • look up the offset in wmi and add it to Now()
  • change the "01/01/1970 00:00:00" string, for example for GMT/UTC+1 = "01/01/1970 01:00:00"

The current Unix time:

' check time zone
DateDiff("s", "01/01/1970 00:00:00", Now())

Current Unix time with time zone fix:

function date2epoch(myDate)
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate (myDate)
DateDiff("s", "01/01/1970 00:00:00", CDate(dateTime.GetVarDate (false)))
end function

Wscript.echo date2epoch(Now())

date2epoch converts a VBScript date to an Unix timestamp:

' check time zone if your date is not GMT/UTC
function date2epoch(myDate)
date2epoch = DateDiff("s", "01/01/1970 00:00:00", myDate)
end function

Usage example:

date2epoch(Now())

epoch2date converts Unix timestamps back to VBScript dates:

' result is GMT/UTC
function epoch2date(myEpoch)
epoch2date = DateAdd("s", myEpoch, "01/01/1970 00:00:00")
end function

JavaScript routines

Convert an epoch to human readable date:

var myDate = new Date( your epoch date *1000);
document.write(myDate.toGMTString()+"<br>"+myDate.toLocaleString());

The example above gives the following output (with epoch date 1):

You can also use getFullYear, getMonth, getDay etc. See documentation below.

Convert human readable dates to epoch:

var myDate = new Date("July 1, 1978 02:30:00"); // Your timezone!
var myEpoch = myDate.getTime()/1000.0;
document.write(myEpoch);

The example above gives the following output:

There are many ways to create dates in Javascript (for example with setFullYear, setMonth, setDay etc.).
For more information on the JavaScript Date object click here.


Comments and questions