Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Console Clock

clock console

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 Netsurfer2

Netsurfer2

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 September 2015 - 08:59 PM

I'm trying to create a clock in the C# console.

 

So far I have created the clock, but have been unable to create a system timer that will work correctly. 

 

I need to display the time in three places while using pause and have it update just like a real clock.

 

The following code works correctly to display, but now I need to add a system timer in order for it to update. 

I'm having trouble adding the timer. 

 

I know I need to use NameSpace: 

 

using System.Timers; 

 

Can anyone please help?

 

DateTime time = System.DateTime.Now;
            String Time1; //Set the String variable into memory.
            String Time2; //Set the String variable into memory.
            String Time3; //Set the String variable into memory.
 
Time1 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the first page of the console.
Time2 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the second page of the console.
Time3 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the third page of the console.
 
Console.WriteLine(Time1); //Write the Date and Time.
Console.WriteLine(Time2); //Write the Date and Time.
Console.WriteLine(Time3); //Write the Date and Time.
 
Thank you!!!

Edited by Netsurfer2, 26 September 2015 - 09:04 PM.


#2 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 25 October 2015 - 06:05 PM

You will want to create a method with the proper signature to match the delegate defined for the Elapsed event handler: https://msdn.microso...(v=vs.110).aspx

Assign that method to the event, and this method should update the time. Make sure to also set the Interval property accordingly.

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#3 Darkhog

Darkhog

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 06 November 2015 - 08:18 PM

You can also put it in while(true) loop, although then you'll have to clear console on each iteration. So the code would be:

while (true) {
  Console.Clear();
  DateTime time = System.DateTime.Now;
            String Time1; //Set the String variable into memory.
            String Time2; //Set the String variable into memory.
            String Time3; //Set the String variable into memory.
 
  Time1 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the first page of the console.
  Time2 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the second page of the console.
  Time3 = time.ToString("                                     MM-dd-yyyy HH:mm tt");  //String Format to write on the third page of the console.
 
  Console.WriteLine(Time1); //Write the Date and Time.
  Console.WriteLine(Time2); //Write the Date and Time.
  Console.WriteLine(Time3); //Write the Date and Time.
}