Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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

Share this answer
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.
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900