TechieClues TechieClues
Updated date Jul 01, 2023
This C# program retrieves the current date and time using the DateTime class and displays them to the user. It showcases the retrieval of the current date and time and provides the output in a specific format.

This program retrieves the current date and time and displays it to the user. It showcases how to obtain the current date and time using C#'s DateTime class. The program outputs the current date and time.

using System;

class CurrentDateTime
{
    static void Main()
    {
        Console.WriteLine("Current Date and Time");
        Console.WriteLine("---------------------");

        DateTime currentDateTime = DateTime.Now;

        Console.WriteLine("Current Date: " + currentDateTime.ToString("yyyy-MM-dd"));
        Console.WriteLine("Current Time: " + currentDateTime.ToString("HH:mm:ss"));
    }
}

The program starts by displaying the title and a description. It uses the DateTime.Now property to retrieve the current date and time, which is assigned to the currentDateTime variable. The program then outputs the current date using the format "yyyy-MM-dd" and the current time using the format "HH:mm:ss". The format strings "yyyy" represents the year, "MM" represents the month, "dd" represents the day, "HH" represents the hour in 24-hour format, "mm" represents the minute, and "ss" represents the second.

Output:

Current Date and Time
---------------------
Current Date: 2023-07-02
Current Time: 09:25:41

In this example, the program retrieves the current date and time and displays them. The current date is shown in the format "yyyy-MM-dd" (e.g., 2023-07-02), and the current time is displayed in the format "HH:mm:ss" (e.g., 09:25:41).

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!