Click here to Skip to main content
14,603,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi.. please check this code of mine:

protected void Button4_Click(object sender, EventArgs e)
        DateTime dt1;
        DateTime dt2;

        string currenttime1 = DateTime.Now.ToLongTimeString();
        string currenttime2 = DateTime.Now.ToLongTimeString();
        TextBox2.Text = currenttime2;

        dt1 = Convert.ToDateTime(currenttime1);
        dt2 = Convert.ToDateTime(currenttime2);
        TimeSpan ts = dt1 - dt2;

        int hours = ts.Hours;
        int min = ts.Minutes;

        string finalMin = Convert.ToString(min);

        TextBox3.Text = finalMin;

---- i'm having a web page that calculated time duration when the user press the stop button.. but the output of this code is always zero.. what is my error?
thanks in advance.. :)
--im using and C# :confused:
Updated 3-Apr-10 3:09am
Rate this:
Please Sign up or sign in to vote.

Solution 2

This is a quick console application which you can adapt to your ASP.NET page.
using System;

namespace TimeDemo
    class Program
        static void Main(string[] args)
            DateTime now = DateTime.Now;
            DateTime earlier = now.AddHours(-1);
            Console.WriteLine("Now: {0}", now.ToLongTimeString());
            Console.WriteLine("Earlier: {0}", earlier.ToLongTimeString());

            TimeSpan duration = now - earlier;
            Console.WriteLine("Duration {0} minutes", duration.TotalMinutes);

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are comparing two times retrieved at virtually the same instance, of course the difference will be 0

Heaven23 wrote:
dt1 = Convert.ToDateTime(currenttime1);
dt2 = Convert.ToDateTime(currenttime2);

This bit is absolutely useless. You are getting the current DateTime, converting it to a string, then converting back to a DateTime. :rolleyes: Useless.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100