Executing a DateTime method in Main

datetime format
c# datetime set time
datetime c#
c# datetime now
c# timespan
c# new datetime
c# get current timestamp
c# datetime compare

I need to make a simple program that asks you to press a button twice, once to start the timer and second time to stop it, then it has to show you the difference in seconds from the moment you pressed these buttons, yet I can't figure out a way to write the code in Main. I think my class Stoper is made properly, but I am not really sure.

I've tried different ways to use ReadKey to assign the starting DateTime to a variable.

class Stoper
{
    DateTime czas_zakonczenia;
    DateTime czas;
    DateTime czas_rozpoczecia;

    private DateTime Czas_rozpoczecia
    {
        get
        {
            return Czas_rozpoczecia;
        }
    }

    public DateTime Start(DateTime czas_rozpoczecia)
    {
        return czas_rozpoczecia = DateTime.Now;
    }

    public DateTime Stop(DateTime czas_zakonczenia)
    {
        return czas_zakonczenia = DateTime.Now;
    }
    public void WyswietlCzas()
    {
        var czas = (Start(czas_rozpoczecia) - Stop(czas_zakonczenia)).TotalSeconds;

    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Kliknij dowolony przycisk gdy chcesz zaczac pomiar czasu.");
        Stoper s1 = new Stoper(Start());
    }
}

You don't need a custom class for that. You can use the StopWatch class. Try something like this:

static void Main(string[] args)
{
    Console.WriteLine("Press any key to start the timer");
    Console.ReadKey();

    Stopwatch sw = Stopwatch.StartNew();
    Console.WriteLine("Timer started. Press any key to stop");
    Console.ReadKey();

    sw.Stop();
    Console.WriteLine("The timer has stopped.");
    Console.WriteLine($"Elapsed time is: {sw.Elapsed.TotalSeconds} seconds.");
    Console.ReadLine();
}

If you have to use a custom class. There are some irrelevant variables in your class and the logic is also incorrect. You may rewrite your class into something like this: (feel free to translate the variables/method names into your language)

class Stoper
{
    public TimeSpan ElapsedTime { get; set; }

    private DateTime startDate;

    public void Start()
    {
        startDate = DateTime.Now;
    }

    public void Stop()
    {
        ElapsedTime = DateTime.Now - startDate;
    }
}

Then, you can use it in the same way the StopWatch class is used above:

static void Main(string[] args)
{
    Console.WriteLine("Press any key to start the timer");
    Console.ReadKey();

    Stoper s1 = new Stoper();
    s1.Start();
    Console.WriteLine("Timer started. Press any key to stop");
    Console.ReadKey();

    s1.Stop();
    Console.WriteLine("The timer has stopped.");
    Console.WriteLine($"Elapsed time is: {s1.ElapsedTime.TotalSeconds} seconds.");
    Console.ReadLine();
}

DateTime Struct (System), Using Visual Basic-specific language features to instantiate a DateTime . Run. var dat1 = new DateTime(); // The following method call displays 1/1/0001  The DateTime.GetDateTimeFormats() method in C# is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers. Syntax Following is the syntax −

Use the Stopwatch class:

private static void Main()
{
    Stopwatch stopWatch = new Stopwatch();

    Console.WriteLine("Press any key to start the timer.\n");
    Console.ReadKey();

    stopWatch.Start();

    Console.WriteLine("Press any key to stop the timer.\n");
    Console.ReadKey();

    stopWatch.Stop();

    Console.WriteLine($"Elapsed seconds: {stopWatch.Elapsed.TotalSeconds}");
    Console.ReadKey();
}

C# DateTime Examples, AddDays, Add. We can add values to a DateTime with methods like AddDays, that uses Add methods using System; class Program { static void Main() { var year2K = new The output will naturally vary depending on the day you run it. DateTime class contains lot of methods, that are used to perform date and time related operations. There are following methods of DateTime are used in C#: DateTime.DaysInMonth() DateTime.IsLeapYear() DateTime.Equals() DateTime.Compare() DateTime.Now; 1) DateTime.DaysInMonth() This method is used to get number of days in given month of year.

private static void Main(string[] args)
    {
        Console.WriteLine("Press any key to Start Timer");
        Console.ReadKey();
        Console.WriteLine("\nTimer Started");
        var startTime = DateTime.Now;
        Console.WriteLine("Press any key to Stop Timer");
        Console.ReadKey();
        var stopTime = DateTime.Now;
        var sb = "\n" + (stopTime - startTime);
        Console.WriteLine(sb);
    }

Date and time in C# - working with date and time in CSharp, dotnet run Today's date: 11/5/2019 12:00:00 AM Today is 5 day of December DateTime has methods for doing time arithmetic operations. Program { static void Main(string[] args) { DateTime dt = new DateTime(2019, 2, 22  Another application that is executing in the U.S. Eastern Time zone then uses the FromBinary method to convert the binary value to a new DateTime object. The value of the new DateTime object is 6 P.M., which represents the same point in time as the original 3 P.M. value, but is adjusted to local time in the Eastern Time zone.

There are several problems in your code:


This causes a stack overflow. Because you use the property (with the capital C), where you want to use the field (with the small c). C# is case sensitive.

    private DateTime Czas_rozpoczecia
    {
        get
        {
            return Czas_rozpoczecia; 
        }
    }

You probably want this:

    private DateTime Czas_rozpoczecia
    {
        get
        {
            return czas_rozpoczecia; 
            //     ^ Small letter
        }
    }

Or even this:

    private DateTime Czas_rozpoczecia => czas_rozpoczecia;

You use an argument with the same name as the field? Then you assign a value (to the argument?) and forget about it.

    public DateTime Start(DateTime czas_rozpoczecia)
    {
        return czas_rozpoczecia = DateTime.Now;
    }

You probably want to use it like this. Store the current DateTime in the field. And you should fix this for the Stop method as well.

    public DateTime Start()
    {
        czas_rozpoczecia = DateTime.Now;
    }

The difference between two DateTimes is a TimeSpan, and there's no use to store it, since you have all fields already.

    public Double WyswietlCzas()
    {
        return (czas_rozpoczecia - czas_zakonczenia).TotalSeconds;

    }
}

Now fix the main code. Use ReadKey to wait for a key. And call the methods using the instance (s1) you just created.

        var s1 = new Stoper();
        Console.ReadKey();
        s1.Start();
        Console.ReadKey();
        s2.Stop();

And you should be able to figure out how to print the seconds yourself.

Python DateTime, TimeDelta, Strftime(Format) with Examples, function. The datetime classes in Python are categorized into main 5 classes. Consider the following points before executing the datetime code The date.​today() function also gives you the weekday number. Here is the  Step 1) Like Date Objects, we can also use "DATETIME OBJECTS" in Python. It gives date along with time in hours, minutes, seconds and milliseconds. When we execute the code for datetime, it gives the output with current date and time. Step 2) With "DATETIME OBJECT", you can also call time class.

Visual Studio Lightswitch 2012, Nhere( Function(issueItem) issueItem.CreateDateTime > startYear). Execute().​C0unt() Cit: File: HelpDeskCS\Client\UserCode\EngineerDetail.cs DateTime  Java main() method. The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program. The syntax of the main() method is: public: It is an access specifier. We should use a public keyword before the main() method so that JVM can identify the execution point of the program.

Java Date and Time, Run example » LocalDateTime class, and use its now() method: public static void main(String[] args) { LocalDateTime myObj = LocalDateTime.now();  You may rewrite your class into something like this: (feel free to translate the variables/method names into your language) class Stoper { public TimeSpan ElapsedTime { get; set; } private DateTime startDate; public void Start() { startDate = DateTime.Now; } public void Stop() { ElapsedTime = DateTime.Now - startDate; } }

Working With DateTime Using C#, The C# DateTime structure represents dates and time in C# and . DateTime structure provides methods to add and subtract date and time to and from a DateTime object. Globalization;; class TimeZoneDemo; {; static void Main( ); {; const which varies depending on the " +; "time zone in which it is run. Overview. Maven exec plugin lets you run the main method of a Java class in your project, with the project dependencies automatically included in the classpath. This article show you 3 ways of using the maven exec plugin to run java, with code examples.

Comments
  • czas = (czas_rozpoczecia - czas_zakonczenia).TotalSeconds. Or czas = (czas_rozpoczecia - Stop(czas_zakonczenia).TotalSeconds. It depends on what u need.
  • Stoper s1 = new Stoper(); Console.Read(); s1.Start();
  • The point is - I need to do that with a custom class and methods.
  • @Wurfv_ I updated the answer with an example of a custom "Stopper" class.
  • And yet, no explanation as to why the OP code wasn't working? If these details were included, it would help the OP and or anyone else coming along with the same issue, know what the problem was and how it can be addressed.
  • @Çöđěxěŕ Feel free to add your own answer.
  • @AhmedAbdelhameed I don't need to, just adding some constructive criticism.
  • Thanks for explaining the OP issue and how to address it.
  • @Çöđěxěŕ Using the Stopwatch class does not teach the OP to program.
  • it's not just about the Stopwatch class; I didn't say it was. My point is you explained the OP issue's they had in code, this give's them an understanding of what they did wrong, followed with a solution.