• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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