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

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash


 Top ASP 101 Stories Top ASP 101 Stories
VBScript Classes: Part 1 of N
Migrating to ASP.NET
Getting Scripts to Run on a Schedule

QUICK TIP:
ASP and Personal Web Server
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")
%>
<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)


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES