We help IT Professionals succeed at work.

How do I get the Time in UNIX Time format from within an ASP page?

kburney asked
I need to compare a UNIX Time formatted value with the current time.  How do I get the UNIX time format for the current time from within an ASP.  UNIX time format is the number of seconds since UTC 1/1/70.

Thanks in advance
Kevin Burney
Watch Question

Use Datediff function like

Function DiffADate(theDate)
   DiffADate = "Days from today: " & DateDiff("s", Now, theDate)
End Function

MSDN Manual

Returns the number of intervals between two dates.

DateDiff(interval, date1, date2 [,firstdayofweek[, firstweekofyear]])
The DateDiff function syntax has these parts:

Required. String expression that is the interval you want to use to calculate the differences between date1 and date2. See Settings section for values.
date1, date2
Required. Date expressions. Two dates you want to use in the calculation.
Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values.
Optional. Constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. See Settings section for values.

Actually with your requirement it should be like

theDate= cdate("1/1/70")
DiffASecond = "Seconds from 1/1/70: " & DateDiff("s", Now, theDate

Response.Write DiffASecond
Sorry a missing bracket

theDate= cdate("1/1/70")
DiffASecond = "Seconds from 1/1/70: " & DateDiff("s", Now, theDate)

Response.Write DiffASecond
Knew you'd get there eventually jitganguly :-)
Hey, I am here after a long time boss


The answer is exactly what I was looking for, thank you.  Now I have another very minor issue and that is how do I get the Now to bring back the time in GMT (UTC)?  The value that I am comparing against (the UNIX formatted time) is in GMT.  I want this code to be portable across time zones.

You can use DateAdd function to get GMT time. If you are in USA EST ( I am at NY area), then you could subtract -5 with dateadd function like

NewDate = DateAdd("h", -5, "31-Jan-95")
and I don't think VBScript has UTC date funtion. But Javascript has.
You may want to pass the ASP VBScript var to Javascript to get UTC Date Format.

Check out this Jscript example from MSDN

The following example illustrates the use of the UTC method.

function DaysBetweenDateAndNow(yr, mo, dy){
   var d, r, t1, t2, t3;            //Declare variables.
   var MinMilli = 1000 * 60         //Initialize variables.
   var HrMilli = MinMilli * 60
   var DyMilli = HrMilli * 24
   t1 = Date.UTC(yr, mo - 1, dy)    //Get milliseconds since 1/1/1970.
   d = new Date();                  //Create Date object.
   t2 = d.getTime();                //Get current time.
   if (t2 >= t1)
      t3 = t2 - t1;
      t3 = t1 - t2;
   r = Math.round(t3 / DyMilli);
   return(r);                       //Return difference.
Mark FranzProject Manager


Man I'm getting slow in my old age...


That is how I am currently doing it.  I was looking for a way to make the ASP work in any time zone at any time of the year (i.e. Daylight savings).  Isn't there some way I can Add/Subract the current machine's timezone offset?  If so how do I reference it.



Sorry, I had not refreshed the screen since your last post.

I had found the JavaScript code to do this and was wondering if I could access the Timezone Offset from VBScript.  The sample javascript that I had found has reference to the Timezone offset.  The code is below.
gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)

<!-- hide from old browsers

    function gmtClock(){
    /* Basic gmt clock /*
    time = new Date()
    gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
    gmtTime = new Date(gmtMS)
    hour = gmtTime.getHours()
    minute = gmtTime.getMinutes()
    second = gmtTime.getSeconds()
    temp = "" + ((hour < 10) ? "0" : "") + hour
    temp += ((minute < 10) ? ":0" : ":") + minute
    temp += ((second < 10) ? ":0" : ":") + second
    document.clockForm.digits.value = temp
<BODY ONLOAD="gmtClock()">
<!-- -->
<FORM NAME="clockForm">
<FONT face="Times new Roman" size=2>
<INPUT TYPE="Text" NAME="Digits" SIZE=8>
</FORM><!-- -->

Explore More ContentExplore courses, solutions, and other research materials related to this topic.