Creating DateTimes
Now, Today, and specific dates.
Meet DateTime
In C#, the DateTime struct represents a single point in time, including both the date and the time of day.
It lives in the System namespace, so it is available in every program. You will use it for birthdays, timestamps, deadlines, and more.
A DateTime value holds a year, month, day, hour, minute, second, and even fractions of a second.
DateTime.Now
The simplest way to get the current date and time is DateTime.Now. It reads your computer's local clock.
Each time you call it, you get a fresh snapshot of the moment. Because the result depends on when it runs, the printed value changes every time.
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine(now);
}
}All lessons in this course
- Creating DateTimes
- Formatting Dates
- TimeSpan and Durations
- Date Math