-
Notifications
You must be signed in to change notification settings - Fork 2
CSharp Basics ConsoleWriteLine DateTime
Joe Care edited this page Dec 22, 2025
·
1 revision
This lesson introduces two fundamental building blocks of C# console programs:
-
Console.WriteLinefor output -
DateTimefor working with dates and times
Related wiki pages:
- General C# overview:
CSharpBible.md - Console helper libraries:
CSharpBible-Library-ConsoleLib.md
Official documentation:
-
Console: https://learn.microsoft.com/dotnet/api/system.console -
DateTime: https://learn.microsoft.com/dotnet/api/system.datetime
using System;
public class Example
{
public static void Main(string[] args)
{
// Create a DateTime object for a specific date
DateTime now = new DateTime(2024, 12, 6); // 6 December 2024
// Print the date
Console.WriteLine("Today is: {0}", now);
}
}Explanation:
-
using System;– Imports theSystemnamespace with basic types likeConsoleandDateTime. -
public class Example– Defines a class namedExample. -
public static void Main(string[] args)– Entry point of the program. -
new DateTime(2024, 12, 6)– Constructs a specific date. -
Console.WriteLine("Today is: {0}", now);– Writes formatted text to the console.
With the .NET SDK you typically use a project instead of calling csc directly:
- Create a new console app:
dotnet new console -n DateTimeSample
- Replace the contents of
Program.cswith the example above. - Run:
dotnet run
You should see something like:
Today is: 06.12.2024 00:00:00
The exact format depends on your system culture.
You can format dates using ToString with format strings:
DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("yyyy-MM-dd")); // 2024-12-06
Console.WriteLine(now.ToString("dd.MM.yyyy")); // 06.12.2024
Console.WriteLine(now.ToString("O")); // ISO 8601See the DateTime docs for a full list of standard and custom format strings.
- Use
Console.ReadLineto read user input and print it back. - Combine
ConsoleI/O with control structures shown in other lessons in this wiki (seeCSharpBible.mdfor navigation). - Explore helper utilities in
CSharpBible-Library-ConsoleLib.mdif available.