ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
An Overview of ASP.NET
Connections, Commands, And Procedures
What is ASP?

Installing IIS on Windows Vista Home Premium
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Expiration Date Calculator

by John Peterson

Some scripts you have to write... others you write because they're interesting or just plain cool... and finally there are scripts like this... scripts that don't do much and take longer to write then it would have taken you to just do what you wrote the script to do!

Let me explain... I'm lazy. (Wow that was easier to explain then I thought!) No really... we've got this thing called security that we, as a company, have to worry about. As part of this security thing, it's company policy that certain passwords get changed on a regular basis. The time period in question ends up being 45 days. To make matters worse... I can't change my own password. There really is a good explanation as to why, but it's a long one and doesn't really pertain to the issue at hand. So anyway... every 45 days or so I email someone in our services department to assign me a new (complex and impossible to remember) password. Once I receive and commit said password to memory, I figure out the date it will expire and write it down on a Post-it Note that I stick to the edge of my monitor so I will remember to renew the password before it expires. (There's nothing like being locked out of your own web server!)

Here's where that lazy thing comes in. (After reading this, you might be inclined to change that label to crazy, but again I digress.) In order to figure out the expiration date I could simply look at my calendar and count off 45 days. I could also say "the heck with it" and figure 42 days is close enough and simply count off 6 weeks. (For the mathematically challenged: 7 days in a week times 6 weeks = 42 days). And yet... although I have no idea why it should... doing either of these simple tasks annoys me. So... we've arrived at the point of this script which (if you haven't already guessed) is to figure out when my password is going to expire!

For those of you in the process of looking up the number of the local mental institution, in my defense, I already had my own custom start page running on my local web server with a number of little tools already on it. (FYI: phone message taker, phone number query, router config screens, forms for my favorite search engines, a page containing dns, ip addresses, and connection details to all the servers I deal with, and a table of the links I use on a daily basis). It's my own personal start page. As such... adding a little expiration date script wasn't a big deal and fits right in with the way I work.

The UI

By now most of you are probably aware of my great flair for user interface design.   ;)  Here's a screen cap:

Screen capture of my marvelous UI work

The Code

Nothing really revolutionary here... not even very pretty code, but it does work so here it is:

<%@ Language="VBScript" %>
<% Option Explicit
Dim datStartDate
Dim intUnitsToAdd
Dim strUnits
datStartDate  = Request.QueryString("StartDate")
intUnitsToAdd = Request.QueryString("UnitsToAdd")
strUnits      = Request.QueryString("Units")
<title>ASP 101's Expiration Date Calculator</title>
Form defaults are hard-wired to the values I need...
I'm too lazy to do the postback work.  What'd you expect? ;)
<form action="<%= Request.ServerVariables("URL") %>">
Start Date: <input type="text" name="StartDate" value="<%= Date() %>" />
<br />
Time Interval: <input type="text" name="UnitsToAdd" value="45" />
<select name="Units">
  <option value="s">Seconds</option>
  <option value="n">Minutes</option>
  <option value="h">Hours</option>
  <option value="d" selected="true">Days</option>
  <option value="ww">Weeks</option>
  <option value="m">Months</option>
  <option value="yyyy">Years</option>
<input type="submit" value="Calculate" />
If strUnits <> "" Then
  ' I know... bad code... render block and all... it works doesn't it?
  Based on a start date of <%= datStartDate %>
  and a time interval of <%= intUnitsToAdd %> <%= strUnits %>,
  the expiration date is:
  <strong><%= DateAdd(strUnits, intUnitsToAdd, datStartDate) %></strong>
End If


As usual... here's a zip file if you prefer: (around 800 bytes)

Update: ASP.NET Version

I received quite a few responses about the above code and they basically fell into 3 categories:

  1. Alternate date formats
  2. Ideas to automate the process more completely (including using Outlook, IIS's SMTP Server, Task Scheduler, etc...)
  3. An ASP.NET version

The alternate date formats are generally easily to implement and I'll leave them as an exercise for the reader since no matter what format I use someone won't be happy. In terms of adding code to more fully automate the process, it's a good idea and I appreciate all the suggestions, but doing it on the web server is cumbersome and sort of inappropriate to the task at hand (aka. managing the password to the server) and the alternatives (using Task Scheduler, Outlook scripts, etc.) sort of fall outside the scope of a site dedicated to ASP. (See... I am lazy... if I can't publish a script then what's the sense in writing it?  ;)  )

Anyway... that brings us to the third item: an ASP.NET version for those of you who have completely switched over to .NET. Since I couldn't come up with an easy excuse not to write one... here it is. I've used ASP.NET server controls where appropriate and except for the lack of validation (which is missing from the original as well) it's not half bad.

Screen Cap

Another example of my marvelous UI work

The .NET Code

<%@ Page Language="VB" Trace="False" %>
<script Language="VB" Option="Strict" RunAt="Server">
  Sub Page_Load(sender As Object, e As EventArgs)
    If Not Page.IsPostBack
      ' Init Form Values on First Page Load
      calStartDate.SelectedDate = DateTime.Now
      txtUnitsToAdd.Text = 45
      ddlUnits.SelectedIndex = 3
      ' Hide result label
      divResult.Visible = False
    End If
  End Sub
  Sub btnCalculate_Click(sender As Object, e As EventArgs)
    Dim datStartTime  As DateTime
    Dim datEndTime    As DateTime
    Dim intUnitsToAdd As Integer
    ' In case you run into trouble...
    datStartTime  = calStartDate.SelectedDate
    intUnitsToAdd = txtUnitsToAdd.Text
    Select Case ddlUnits.SelectedItem.Text
      Case "Seconds"
        datEndTime = datStartTime.AddSeconds(intUnitsToAdd)
      Case "Minutes"
        datEndTime = datStartTime.AddMinutes(intUnitsToAdd)
      Case "Hours"
        datEndTime = datStartTime.AddHours(intUnitsToAdd)
      Case "Days"
        datEndTime = datStartTime.AddDays(intUnitsToAdd)
      Case "Weeks"
        datEndTime = datStartTime.AddDays(intUnitsToAdd * 7)
      Case "Months"
        datEndTime = datStartTime.AddMonths(intUnitsToAdd)
      Case "Years"
        datEndTime = datStartTime.AddYears(intUnitsToAdd)
    End Select
    lblStartDate.Text = datStartTime
    lblInterval.Text  = intUnitsToAdd & " " & ddlUnits.SelectedItem.Text
    lblResult.Text    = datEndTime
    ' Show result label
    divResult.Visible = True
  End Sub
<title>ASP 101's Expiration Date Calculator</title>
<form runat="server">
Start Date:
<asp:Calendar id="calStartDate" runat="server" />
<br />
Time Interval:
<asp:TextBox id="txtUnitsToAdd" runat="server" />
<asp:DropDownList id="ddlUnits" runat="server">
<asp:Button id="btnCalculate" Text="Calculate"
  onclick="btnCalculate_Click" runat="server" />
<div id="divResult" runat="server">
Based on a start date of
<asp:Label id="lblStartDate" runat="server" />
and a time interval of
<asp:Label id="lblInterval" runat="server" />,
the expiration date is:
<strong><asp:Label id="lblResult" runat="server" /></strong>

The .NET Download

And again... here's a zip file if you prefer: (around 1 KB)

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers