Fecha y hora continua Ejercicio C# - Curso de Programación C# (C Sharp)

 Ejercicio

Fecha y hora continua

 Objetivo

Crear un programa para mostrar la hora actual en la esquina superior derecha de la pantalla, con el siguiente formato:

12:52:03

debe detenerse un segundo y mostrarlo de nuevo, en la misma posición.

 Código de Ejemplo

// Import the necessary namespaces for working with date, time, and console operations
using System;

class Program
{
    // Main method where the program execution starts
    static void Main()
    {
        // Infinite loop to continuously display the time
        while (true)
        {
            // Get the current time
            string currentTime = DateTime.Now.ToString("HH:mm:ss");  // Formats the current time as HH:mm:ss (24-hour format)

            // Move the cursor to the top-right corner of the console window
            Console.SetCursorPosition(Console.WindowWidth - currentTime.Length - 1, 0); // Position at top-right corner

            // Display the current time at the cursor position
            Console.Write(currentTime);  // Writes the current time to the console at the specified location

            // Pause for 1 second before updating the time again
            System.Threading.Thread.Sleep(1000);  // Pauses the program for 1000 milliseconds (1 second)

            // Clear the previous time by moving the cursor back to the top-right corner and overwriting it
            Console.SetCursorPosition(Console.WindowWidth - currentTime.Length - 1, 0);  // Move to the same position
            Console.Write("       ");  // Clear the previous time by overwriting it with blank spaces
        }
    }
}

Más ejercicios C# Sharp de Bibliotecas Adicionales


Juan A. Ripoll - Tutoriales y Cursos de Programacion© 2025 Todos los derechos reservados.  Condiciones legales.