Search
Tuesday, March 09, 2010 ..:: Home ::.. Register  Login
 .NET Links Minimize

 Print   
 My Links Minimize

 Print   
 Recent Blog Entries Minimize
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:

  
 PS: Minimize

 Print   
 Sponsors Minimize

Get your own Komets drinking Stein!

Get your KOMETS gear!
Proceeds go for player awards @ the end of each season.

  Quality Geek Gear from GeeksWithGuns.com

We use DiscountASP!! Click to join up!


 Print   
 Facebook Minimize

Wil Dobson's Facebook profile


 Print   
 Blogs Minimize

 Print   
 Manage Blog Minimize
You must be logged in and have permission to create or edit a blog.

 Print   
 Blog Archives Minimize

 Print   
Copyright 2008 by Wil Dobson   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2010 by DotNetNuke Corporation