0

I am trying to make a stopwatch with C# as an exercise for myself. my plan was to make two methods " start()" and " stop()" then call these from my stopwatch class in my main. the problem I have is that I do not know how to get the time span between these two.

for your information, this is how I want the program to work: if they typed s the timer starts and when press enter or type f the time will be shown to them.

this is the code I have written so far, but got stuck when getting the time span.

 class StopWatch
{
    DateTime starting = DateTime.Now;
    DateTime finishing = DateTime.Now;
    
    
    public void start()
    {
        Console.WriteLine(starting);
    }

    public void stop()
    {
      
        Console.WriteLine(finishing);
    }

    
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("type s to start and f to stop");
        var input = Console.ReadLine();
        var stopwatch = new StopWatch();
        if (input.ToLower() == "s") { stopwatch.start(); }
        var Input2 = Console.ReadLine();
        if (Input2.ToLower() == "f") { stopwatch.stop(); }

        

        Console.ReadKey();
    }
}
Rasta Mosi
  • 87
  • 1
  • 12
  • 2
    Do not create your own class. Use Stopwatch from .net library. All methods are there. – Optional Option May 15 '21 at 14:59
  • 1
    @OptionalOption You are right, that can be done and I saw many tutorials about how to do it that way but currently, I am studying about classes in C# and this is one of the exercises. that's why I want to solve it this way. appreciate your comment though .:) – Rasta Mosi May 15 '21 at 15:03
  • 1
    Just `Elapsed = finishing - starting;` in the Stop. It is a [TimeSpan](https://docs.microsoft.com/dotnet/api/system.timespan) – Olivier Rogier May 15 '21 at 15:06
  • You can use TimeSpan in c# which will give the difference b/w two dates. For example TimeSpan ts = t2-t1; here t2 and t1 are dates – Rahul Shukla May 15 '21 at 15:07
  • Does this answer your question? [Showing Difference between two datetime values in hours](https://stackoverflow.com/questions/4946316/showing-difference-between-two-datetime-values-in-hours) – Olivier Rogier May 15 '21 at 15:07
  • I forgot: in the `Stop` set `Finished = DateTime.Now`. Also you can create a property to read `Elapsed` in realtime instead that returns `DateTime.Now - Started` if running else `Finished - Started`. You will need a field `bool Running`. On Start you can continue and Restart reset the Stared to Now. Thus you can do pauses. – Olivier Rogier May 15 '21 at 15:14

1 Answers1

6

I agree with the comment to use what already exists in the library, but since you said you are doing this as an exercise, here is some feedback:

To answer you direct question how to get a TimeSpan: var duration = finishing - starting;

The current implementation will not do what you intend to do, since you set both starting and finishing at object creation time: field initializers are executed before any constructor code. So you should set starting in the start() method and finishing in the stop() method. Then you can calculate the duration as shown above in the stop() method, too.

And allow me a little side note on naming: "starting" and "finishing" are progressive forms in English, but here you want to name specific values. Therefore I'd recommend "startTime" and "endTime"/"stopTime".

AlWo23
  • 74
  • 3