C# DateTime

Beginner
7 min read
Updated: Nov 2026
3 examples
DateTime

Definition and Usage

In C#, System.DateTime is the built-in type for a date and a time of day. You can build one with new DateTime(2026, 11, 18), move it with AddDays(), read text with TryParse(), and write text with ToString().

A DateTime counts time in ticks (one tick is 100 nanoseconds). It also has a Kind: Utc, Local, or Unspecified. The tutorials in this series use fixed dates so the output never changes.

This page introduces DateTime. The DateTime Methods index lists every method guide in this series.

Immutable value.

Methods such as AddDays never change the original. They return a new DateTime. If you forget to assign the result, your date will not move.

C# Syntax

Common first steps:

C#
DateTime d1 = new DateTime(2026, 11, 18);
DateTime d2 = new DateTime(2026, 11, 18, 14, 30, 45);
DateTime d3 = d1.AddDays(7);
string text = d2.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
// DateTime.Now and DateTime.UtcNow read the clock (output changes every run)

DateTime vs DateOnly and TimeOnly

Pick the smallest type that fits your data (DateOnly and TimeOnly need .NET 6 or later):

TypeStoresGood for
DateTimeDate and time of day, plus KindTimestamps, deadlines, logs
DateOnlyCalendar date onlyBirthdays, holidays, due dates
TimeOnlyTime of day onlyOpening hours, alarms

C# Examples

Three short programs that use a fixed date.

Example 1 — Create a Date and Print It

A first program that builds a DateTime from year, month, and day, then prints parts of it.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        Console.WriteLine(dt.Year);
        Console.WriteLine(dt.DayOfWeek);
        Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));
    }
}

How It Works

  1. Constructor. new DateTime(2026, 11, 18, 14, 30, 45) sets year, month, day, hour, minute, and second.
  2. Properties. Year is a number and DayOfWeek is a built-in enum value.
  3. Custom format. The pattern yyyy-MM-dd HH:mm:ss prints a fixed, readable text.
  4. Invariant culture. Passing InvariantCulture keeps the text the same on every computer.

Example 2 — Move the Date with AddDays

Add days to a date and notice that the original variable stays the same.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime start = new DateTime(2026, 11, 18);
        DateTime later = start.AddDays(14);
        Console.WriteLine(start.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
        Console.WriteLine(later.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
        Console.WriteLine((later - start).TotalDays);
    }
}

How It Works

  1. Start value. start is November 18, 2026 at midnight.
  2. New value. AddDays(14) returns December 2; it does not edit start.
  3. Both printed. The first line proves the original date is unchanged.
  4. Subtract dates. later - start gives a TimeSpan; TotalDays is 14.

Example 3 — Format a Date in Three Ways

Show a long date, a short time, and a sortable text with fixed patterns.

C#
using System;
using System.Globalization;

class Program {
    static void Main() {
        DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
        CultureInfo inv = CultureInfo.InvariantCulture;
        Console.WriteLine(dt.ToString("D", inv));
        Console.WriteLine(dt.ToString("HH:mm", inv));
        Console.WriteLine(dt.ToString("s", inv));
    }
}

How It Works

  1. Standard long date. "D" prints the weekday name, day, month name, and year.
  2. Custom time. HH:mm gives a 24-hour clock without seconds.
  3. Sortable text. "s" writes yyyy-MM-ddTHH:mm:ss, which sorts correctly as plain text.
  4. One culture object. Reusing inv keeps all three lines predictable.

Next: C# DateTime Methods Index

After you know what DateTime is for, open the method index for a full tutorial on each System.DateTime API.

Start withWhy
AddDays()Move a date forward or back by days
TryParse()Read date text safely, without exceptions
ToString()Format a date for people or files
Subtract()Find the gap between two dates as a TimeSpan

Full list: C# DateTime Methods index (/c-sharp/datetime/method).

Frequently Asked Questions

It is the C# struct that stores a date and a time of day. You create it with new DateTime(year, month, day) and read parts such as Year, Month, Day, and DayOfWeek.
No. DateTime is immutable. AddDays, AddMonths, and the other Add methods return a new DateTime. Assign the result to a variable to keep it.
Use DateTime when you need both date and time in one value. Use DateOnly for a calendar date only, and TimeOnly for a clock time only. Both exist in .NET 6 and later.
Kind tells you if a value is Utc, Local, or Unspecified. It matters when you convert with ToUniversalTime or ToLocalTime, and when you save or send dates.
Open the C# DateTime Methods index. It lists AddDays, Parse, TryParse, ToString, Subtract, and every other method guide.

Browse every DateTime method

Open the methods index for categorized guides with examples and FAQs.

DateTime Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful