Creating DateTimes
Now, Today, and specific dates.
Creating DateTimes is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}Today's Date
If you only care about the date and not the time, use DateTime.Today. It returns midnight (00:00:00) of the current day.
This is handy when comparing days, since the time portion is set to zero.
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Today;
Console.WriteLine(today.Year);
}
}UTC Time
Computers around the world share UTC (Coordinated Universal Time). Use DateTime.UtcNow to get the current time in UTC, free of any local time zone.
UTC is the safe choice for servers and for storing timestamps that many regions will read.
using System;
class Program
{
static void Main()
{
DateTime utc = DateTime.UtcNow;
Console.WriteLine(utc.Kind);
}
}Building a Specific Date
To create an exact date, use the constructor new DateTime(year, month, day). The time portion defaults to midnight.
This is perfect for fixed dates like a holiday or a launch day. Because the value is fixed, the output is fully predictable.
using System;
class Program
{
static void Main()
{
DateTime launch = new DateTime(2024, 1, 15);
Console.WriteLine(launch.ToString("yyyy-MM-dd"));
}
}Adding the Time of Day
A longer constructor lets you set hours, minutes, and seconds: new DateTime(year, month, day, hour, minute, second).
Hours use the 24-hour clock, so 14 means 2 PM. This gives you a full timestamp down to the second.
using System;
class Program
{
static void Main()
{
DateTime meeting = new DateTime(2024, 3, 5, 14, 30, 0);
Console.WriteLine(meeting);
}
}Reading the Parts
Once you have a DateTime, you can read each piece through properties: Year, Month, Day, Hour, Minute, and Second.
These return plain integers, so they are easy to print or compare.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 3, 5, 14, 30, 0);
Console.WriteLine(d.Month);
Console.WriteLine(d.Hour);
}
}Day of the Week
The DayOfWeek property tells you which weekday a date falls on. It returns a value of the DayOfWeek enum, such as Monday or Saturday.
There is also DayOfYear, an integer from 1 to 366.
using System;
class Program
{
static void Main()
{
DateTime d = new DateTime(2024, 1, 1);
Console.WriteLine(d.DayOfWeek);
}
}Min and Max Values
DateTime has two special constants. DateTime.MinValue is January 1, year 1, and DateTime.MaxValue is December 31, year 9999.
They are useful as default or sentinel values when no real date is set yet.
using System;
class Program
{
static void Main()
{
Console.WriteLine(DateTime.MinValue);
Console.WriteLine(DateTime.MaxValue.Year);
}
}Parsing a Date String
Text from a user or a file often holds a date. DateTime.Parse turns a string into a DateTime.
If the text is not a valid date, Parse throws an error. For safer handling you can use DateTime.TryParse, which returns true or false instead of crashing.
using System;
class Program
{
static void Main()
{
DateTime d = DateTime.Parse("2024-07-04");
Console.WriteLine(d.Year);
}
}Safe Parsing with TryParse
DateTime.TryParse avoids exceptions. It takes the text and an out variable, returning true on success.
This pattern is the recommended way to handle uncertain input, like a value typed by a user.
using System;
class Program
{
static void Main()
{
bool ok = DateTime.TryParse("not a date", out DateTime result);
Console.WriteLine(ok);
}
}Quick Check
You want only the current date with no time component (midnight). Which call should you use?
Recap
You learned how to create DateTime values. Use DateTime.Now for the local moment, DateTime.Today for midnight today, and DateTime.UtcNow for universal time.
Build fixed dates with new DateTime(...), read parts via Year, Month, and DayOfWeek, and convert text with Parse or the safer TryParse.
Frequently asked questions
Is the “Creating DateTimes” lesson free?
Yes — the full text of “Creating DateTimes” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating DateTimes”?
Now, Today, and specific dates. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating DateTimes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating DateTimes
- Formatting Dates
- TimeSpan and Durations
- Date Math