Apr
9
Written by:
Wil Dobson
4/9/2008 7:39 AM
To make this work, you will need to either start some timer at the initialization of your page and stop it when your page has finished loading into the browser, or you need to get a pair of snapshots in time (start and end) and then determine the TimeSpan. For the following example, I opted to break the code out into a separate assembly class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web;
namespace WilsToolbox
{
public class DateTimeHelper
{
public enum TimeSpanSelection { MilliSeconds, Seconds, Minutes, Hours, Days, Months, Years }
public static void PageLoadTimerStart(HttpContext context)
{
DateTime oldDate = DateTime.Now;
// save it to the session
context.Session["PageLoadStartTime"] = oldDate;
}
public static double PageLoadTimerStop(HttpContext context, TimeSpanSelection timeSpan)
{
// now get another snapshot in time
DateTime newDate = DateTime.Now;
// Determine the time interval with TimeSpan
TimeSpan ts = newDate - (DateTime)context.Session["PageLoadStartTime"];
double difference = 0;
switch (timeSpan)
{
case TimeSpanSelection.MilliSeconds:
difference = ts.TotalMilliseconds;
break;
case TimeSpanSelection.Seconds:
difference = ts.TotalSeconds;
break;
case TimeSpanSelection.Minutes:
difference = ts.TotalMinutes;
break;
case TimeSpanSelection.Hours:
difference = ts.TotalHours;
break;
case TimeSpanSelection.Days:
difference = ts.TotalDays;
break;
case TimeSpanSelection.Months:
difference = ts.TotalDays / 7;
break;
case TimeSpanSelection.Years:
difference = ts.TotalDays / 365;
break;
}
return difference;
}
}
}
To test this code, ca
Copyright ©2008 Wil Dobson
Tags: