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).
Comments (0)