Calculating Dates & Time


There come a time where you will need to do some date calculations. There are several ways of handling this; we'll cover the basics on this page. We'll assume that we're performing calculations using today's date, 02/15/2000.

Let's say we want to know how many days are left until some date in the future - for this example, let's use the real (LOL) start of the new millenium: January 1, 2001.

There are 11 months until January 1, 2001.            <% DateDiff("m",date,"1/1/01") %>
There are 45 weeks until January 1, 2001.              <% DateDiff("w",date,"1/1/01") %>
There are 321 days until January 1, 2001.               <% DateDiff("d",date,"1/1/01") %>
There are 7704 hours until January 1, 2001.           <% DateDiff("h",date,"1/1/01") %>
There are 462240 minutes until January 1, 2001.      <% DateDiff("n",date,"1/1/01") %>
There are 27734400 seconds until January 1, 2001.  <% DateDiff("m",date,"1/1/01") %>

For simpler calculations, such as when you need to add or subtract a set number of days from a date, we can make it even simpler. For example, let's say we want to know what the date was 90 days ago.

The date 30 days ago was 01/16/2000.         <% Date - 90 >
In 30 days, the date will be 03/16/2000.        <% Date + 30 %>

As a final example, let's figure out how old I am today:
Kathi is 43.1666666666667 years old.
<%
Age = DateDiff("m","12/25/56",Date)
CurrentAge = Age/12
%>

This particular solution may not be exactly what you want. I, for one, do not want to be reminded that I'm actually older than just the integer portion of my age (LOL). So, let's just display the whole number and leave off the decimal part of the age:

Kathi's age is 43.
The code: <% Fix(CurrentAge) %>

Note: Both Fix and Int return just the integer portion of a number. If the number is negative, Fix will return the first number greater than or equal to a number, while Int will return the first number less than or equal to a number.