• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How do I get the elapsed time between two time objects (Asp.net, c#)

I need to create a timer.  When the button is  pressed it get gets the time.  Then when the stop button is pressed it stops and give the amount of time that has elapsed in minutes.

This is the code I have now:

public DateTime dt = DateTime.Now;
    protected void Page_Load(object sender, EventArgs e)
    {
      Label1.Text = dt.ToLongTimeString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DateTime dts = DateTime.Now;
       
        TimeSpan elaps = dts.Subtract(dt);
        Label2.Text = elaps.TotalMinutes.ToString();
       
    }

Open in new window

0
bmanmike39
Asked:
bmanmike39
  • 5
  • 2
1 Solution
 
viralypatelCommented:
what's wrong with the code you've pasted in your question? it should work fine.
0
 
bmanmike39Author Commented:
This is what i get when I run the app:  1.66666666666667E-05
and 3.33333333333333E-05
and after a few min 5E-05
i don't understand the results
0
 
viralypatelCommented:
use this:

TimeSpan span = endTime.Subtract ( startTime );
 Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
 Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
 Console.WriteLine( "Time Difference (hours): " + span.Hours );
 Console.WriteLine( "Time Difference (days): " + span.Days );

Open in new window



0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
bmanmike39Author Commented:
Tried this Label3.Text = elaps.Seconds.ToString();  got the result 0
0
 
bmanmike39Author Commented:
This is not working:

TimeSpan span = endTime.Subtract ( startTime );
 Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
 Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
 Console.WriteLine( "Time Difference (hours): " + span.Hours );
 Console.WriteLine( "Time Difference (days): " + span.Days );
0
 
bmanmike39Author Commented:
Code I have now that works

DateTime date1 = DateTime.MinValue;
        DateTime date2 = DateTime.MinValue;

        DateTime.TryParse(x2, out date1);
        DateTime.TryParse(x3, out date2);

        Label6.Text = date1.ToShortTimeString();
        Label7.Text = date2.ToShortTimeString();

I have the two values, but cant get the difference in time between the two.  And I need that in hrs and min, only.
0
 
bmanmike39Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now