Now: Tutorial for Web and Software Design > Web Design > ASP > Web Design Content
> Expiration Date Calculator [Bookmark it]
Expiration Date Calculator

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")

%>

<html>

<head>

<title>ASP 101's Expiration Date Calculator</title>

</head>

<body>



<!--

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>

</select>



<input type="submit" value="Calculate" />



</form>



<%

If strUnits <> "" Then

  ' I know... bad code... render block and all... it works doesn't it?

  %>

  <p>

  Based on a start date of <%= datStartDate %>

  and a time interval of <%= intUnitsToAdd %> <%= strUnits %>,

  the expiration date is:

  <strong><%= DateAdd(strUnits, intUnitsToAdd, datStartDate) %></strong>

  </p>

  <%

End If

%>



</body>

</html>

Download

As usual... here's a zip file if you prefer: expirationdate.zip (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...

    Trace.Write(calStartDate.SelectedDate)

    Trace.Write(txtUnitsToAdd.Text)

    Trace.Write(ddlUnits.SelectedItem.Value)



    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



</script>

<html>

<head>

<title>ASP 101's Expiration Date Calculator</title>

</head>

<body>



<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:ListItem>Seconds</asp:ListItem>

  <asp:ListItem>Minutes</asp:ListItem>

  <asp:ListItem>Hours</asp:ListItem>

  <asp:ListItem>Days</asp:ListItem>

  <asp:ListItem>Weeks</asp:ListItem>

  <asp:ListItem>Months</asp:ListItem>

  <asp:ListItem>Years</asp:ListItem>

</asp:DropDownList>



<asp:Button id="btnCalculate" Text="Calculate"

  onclick="btnCalculate_Click" runat="server" />



</form>



<div id="divResult" runat="server">

<p>

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>

</p>

</div>



</body>

</html>

The .NET Download

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

[Bookmark][Print] [Close][To Top]
  • Prev Article-Web Design:

  • Next Article-Web Design:
  • Related Materias
    Microsoft Expression Web D
    Query Index Server with IX
    ASP.NET 2.0 Compilation Mo
    Using Template Files to Si
    A Sneak Peek at Visual Web
    Extending Your Page Names 
    Using Index Server to Sear
    Using the Google APIs to S
    Working with Google APIs t
    Cheating Google (and Spide
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial