Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Date and Time


Share

Date and Time

Many programs work with dates. A calendar shows today's date, a shop prints the date on a receipt, and a game counts the days until a new level opens.

C# has a built-in type for this: DateTime. A DateTime stores a date and a time together.

To create a date, use new DateTime() with the year, the month and the day:

Example

DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate);  // 3/14/2025 12:00:00 AM

Try it Yourself »

When you don't give a time, the time is set to midnight (12:00:00 AM).

To add a time, write the hour, the minute and the second after the date:

Example

DateTime meeting = new DateTime(2025, 3, 14, 15, 30, 0);
Console.WriteLine(meeting);  // 3/14/2025 3:30:00 PM

Try it Yourself »

The hour goes from 0 to 23, so 15 is 3 PM.


Get Parts of a Date

A DateTime has properties for each part of the date:

Example

DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate.Year);       // 2025
Console.WriteLine(myDate.Month);      // 3
Console.WriteLine(myDate.Day);        // 14
Console.WriteLine(myDate.DayOfWeek);  // Friday

Try it Yourself »

DayOfWeek gives the name of the day. C# works it out for you.


Current Date and Time

DateTime.Now gives the current date and time of the computer.

DateTime.Today gives today's date, with the time set to midnight:

Example

Console.WriteLine(DateTime.Now);
Console.WriteLine(DateTime.Today);

Try it Yourself »

The output is different every time you run the program.



Format a Date

To choose how a date looks, use ToString() with a format. The letters in the format are replaced with parts of the date:

Example

DateTime myDate = new DateTime(2025, 3, 14, 15, 30, 0);
Console.WriteLine(myDate.ToString("yyyy-MM-dd"));         // 2025-03-14
Console.WriteLine(myDate.ToString("HH:mm"));              // 15:30
Console.WriteLine(myDate.ToString("dddd dd MMMM yyyy"));  // Friday 14 March 2025

Try it Yourself »

Other characters in the format, like - and spaces, are printed as they are.

These are the most common format letters:

LettersMeaningExample
yyyyYear2025
MMMonth (2 digits)03
MMMMName of the monthMarch
ddDay (2 digits)14
ddddName of the dayFriday
HHHour (00 to 23)15
mmMinutes30
ssSeconds00

Add Days, Months and Years

AddDays() adds a number of days to a date, and returns the new date:

Example

DateTime start = new DateTime(2025, 3, 14);
DateTime end = start.AddDays(20);
Console.WriteLine(end.ToString("yyyy-MM-dd"));  // 2025-04-03

Try it Yourself »

March has 31 days, so 20 days after March 14 is April 3. C# knows how many days each month has.

AddMonths() adds months, and AddYears() adds years. Use a negative number to go back in time.

In this example, ToString() is called directly on each new date:

Example

DateTime myDate = new DateTime(2025, 3, 14);
Console.WriteLine(myDate.AddMonths(2).ToString("yyyy-MM-dd"));  // 2025-05-14
Console.WriteLine(myDate.AddYears(1).ToString("yyyy-MM-dd"));   // 2026-03-14
Console.WriteLine(myDate.AddDays(-7).ToString("yyyy-MM-dd"));   // 2025-03-07

Try it Yourself »


Days Between Two Dates

When you subtract one date from another, you get a TimeSpan. A TimeSpan stores a length of time.

Its Days property gives the number of whole days:

Example

DateTime today = new DateTime(2025, 3, 14);
DateTime birthday = new DateTime(2025, 7, 1);
TimeSpan wait = birthday - today;
Console.WriteLine(wait.Days);  // 109

Try it Yourself »


Compare Dates

You can compare two dates with the comparison operators, like < and >. An earlier date is smaller than a later date:

Example

DateTime deadline = new DateTime(2025, 3, 31);
DateTime today = new DateTime(2025, 3, 14);

if (today < deadline)
{
  Console.WriteLine("You still have time.");
}
else
{
  Console.WriteLine("Too late!");
}

Try it Yourself »

March 14 is before March 31, so today < deadline is true.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.