C# DateTime class in C# provides properties and methods to format dates in different DateTime formats. This article explains how to work with the C# DateTime format, Including Timer and Sleep Methods.
Let’s start with C# DateTime Object–
DateTime is a Struct in the System namespace. It helps the developers to retrieve information about system date, time, month, year, or even the day of the week.
How To Create The C# DateTime Object?
class Program
{
static void Main(string[] args)
{
// Year, Month, Date
DateTime dt = new DateTime(2016, 03, 07);
Console.WriteLine(dt.ToString());
Console.Read();
}
}
The output of the above program will be: 3/7/2016 12:00:00 AM
If you see here, in this output time is shown as 12:00:00 AM (default time). We haven’t set any time value, and thus the system automatically assigned default values to an hour, minute, and second.
DateTime Object Properties:
DateTime dt = new DateTime(2016, 03, 07);
int year = dt.Year;
int month = dt.Month;
int date = dt.Day;
int hour = dt.Hour;
int min = dt.Minute;
int sec = dt.Second;
int dayOfWeek = Convert.ToInt32(dt.DayOfWeek);
int dayOfYear = dt.DayOfYear;
Year
Year property retrieves the year component of the date represented by the date-time instance. It returns an integer value expressed as a value between 1 and 9999.
Month
Month property retrieves the month component of the date represented by the date-time instance. It returns an integer value expressed as a value between 1 and 12.
Day
Day property retrieves the date component of the month represented by the date-time instance. It returns an integer value expressed as a value between 1 and 31.
Hour
Hour property retrieves the hour component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 23.
Minute
Min property retrieves the minute component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 59.
Second
Second property retrieves the second component of the date represented by the date-time instance. It returns an integer value expressed as a value between 0 and 59.
Day of the Week
Day of the week property retrieves the enumerated constant that indicates the day of the week represented by the date-time object. It also requires casting to accept an integer value.
Day of Year
Day of year property retrieves the day of the year represented by the date-time object. It returns an integer value expressed as a value between 0 and 366.
Now let’s have a look at the code below to see how we can use these properties.
DateTime dt = new DateTime(2016, 03, 07);
int year = dt.Year;
int month = dt.Month;
int date = dt.Day;
int hour = dt.Hour;
int min = dt.Minute;
int sec = dt.Second;
int dayOfWeek = Convert.ToInt32(dt.DayOfWeek);
int dayOfYear = dt.DayOfYear;
Console.WriteLine(year);
Console.WriteLine(month);
Console.WriteLine(date);
Console.WriteLine(hour);
Console.WriteLine(min);
Console.WriteLine(sec);
Console.WriteLine(dayOfWeek);
Console.WriteLine(dayOfYear);
Console.Read();
The output of the above program will be:
Year: 2016
Month : 3
Date: 7
Hour : 0
Minute : 0
Second : 0
Day of the week: 1
Day of the year: 67
How To Get the Current Date and Time?
DateTime object contains a number of different methods to access the system time. The “Now” method allows you to get the current system time/date and even allows you to operate on it.
Syntax:
DateTime dt = DateTime.Now;
We can easily convert it to a string to get the current date-time, or we can even change the format of the date by using the formats below.
NOTE: Consider the current date and time is 7 March 2025 11:06:25
Display date and time in 24-hour format
DateTime.Now.ToString(“dd/MM/yyyy HH:mm:ss”);
Ans :07/03/2025 11:06:25
Note: HH is used for 24 24-hour format.
hh for 12 hour format
Display am/pm in the date and time or 12-hour format.
DateTime.Now.ToString(“hh:mm:ss tt”);
Ans:11:06:25 AM
Note: “tt”
for AM or PM
Display the time zone in the date and time.
DateTime.Now.ToString(“hh:mm:ss tt zzz”);
Ans: 11:06:25 AM +05:30 (Here +05:30 is the time zone for India)
Note: zzz is used to display the time zone.
Show the full month name in the date.
DateTime.Now.ToString(“dd-MMMM-yy”);
Ans: 07-March-16
How to format a full day name in a date?
DateTime.Now.ToString(“dddd-MMMM-yy”);
Ans: Monday-March-16
Note: MMMM for full month name
How to format the full Year name in a date?
DateTime.Now.ToString (“dddd-MMMM-yyyy”);
Ans: Monday-March-2016
Note: yyyy for four-digit year
To reduce/subtract date or days
DateTime.Now.AddDays(-10)
To add days
DateTime.Now.AddDays(10)
C# DateTime to “YYYYMMDDHHMMSS” format
DateTime.Now.ToString (“yyyyMMddHHmmss”);
You can convert the dateTime to any DateTime format by simply adding the required format in ToString (.ToString(“yyyyMMdd”)) as shown in the above example. Please note that C# datetime format is case-sensitive. Please check below.
y = year, m = minutes / M = months, d= date, h = 12 hour, H = 24 hour, s= seconds
Let’s have a look at below C# DateTime formats and their outputs. Here we see all the patterns of the C# DateTime, format, and outputs.
C# DateTime Format | Output |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm”) | 11/29/2019 09:11 |
DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”) | 11/29/2019 09:11 AM |
DateTime.Now.ToString(“MM/dd/yyyy H:mm”) | 11/29/2019 9:11 |
DateTime.Now.ToString(“MM/dd/yyyy h:mm tt”) | 11/29/2019 9:11 AM |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm:ss”) | 11/29/2019 9:11:32 |
DateTime.Now.ToString(“MM/dd/yyyy”) | 11/29/2019 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 November 2019 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm”) | Friday, 29 November 2019 09:11 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy hh:mm tt”) | Friday, 29 November 2019 09:11 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy h:mm”) | Friday, 29 November 2019 9:11 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy h:mm tt”) | Friday, 29 November 2019 9:11 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm:ss”) | Friday, 29 November 2019 09:11:32 |
DateTime.Now.ToString(“MMMM dd”) | November 29 |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK”) | 2019-11-29T09:11:32.0839641+05:30 |
DateTime.Now.ToString(“ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT'”) | Fri, 16 Nov 2019 09:11:32 GMT |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss”) | 2019-11-29T09:11:32 |
DateTime.Now.ToString(“HH:mm”) | 09:11 |
DateTime.Now.ToString(“hh:mm tt”) | 09:11 AM |
DateTime.Now.ToString(“H:mm”) | 9:11 |
DateTime.Now.ToString(“h:mm tt”) | 9:11 AM |
DateTime.Now.ToString(“HH:mm:ss”) | 09:11:32 |
DateTime.Now.ToString(“yyyy MMMM”) | 2019 November |
DateTime.Now.ToString(“yyyy MMM”) | 2019 Nov |
DateTime.Now.ToString(“MM.dd.yyyy HH:mm”) | 11.29.2019 09:11 |
DateTime.Now.ToString(“MM.dd.yyyy hh:mm tt”) | 11.29.2019 09:11 AM |
DateTime.Now.ToString(“MM-dd-yyyy H:mm”) | 11-29-2019 9:11 |
DateTime.Now.ToString(“MM-dd-yyyy h:mm tt”) | 11-29-2019 9:11 AM |
DateTime.Now.ToString(“MM.dd.yyyy HH:mm:ss”) | 11.29.2019 9:11:32 |
DateTime.Now.ToString(“dd/MM/yyyy”) | 29/11/2019 |
C# DateTime Formatting Best Practices
1. Always Specify Culture for Persistent Storage
// For storage/transmission, use invariant culture
string dbFormat = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
// For display, use current culture
string userFormat = DateTime.Now.ToString("F", CultureInfo.CurrentCulture);
2. Performance Optimization for High-Volume Formatting
// Cache formatters for repeated use
var formatter = new StringBuilder(32);
for (int i = 0; i < 1000; i++) {
formatter.Clear();
formatter.Append(DateTime.Now.Year).Append("-")
.Append(DateTime.Now.Month.ToString("00")).Append("-")
.Append(DateTime.Now.Day.ToString("00"));
// Use formatter.ToString()
}
Let’s look at the sample C# code below that uses these C# datetime formats.
namespace DateTimeFormat { class Program { static void Main(string [] args) { // dt erpresents DateTime object. // Here DateTime is a Struct in the System namespace. //year,month,date,hour,minute,seconds DateTime dt = new DateTime(2016, 03, 07, 11, 06, 25); Console.WriteLine("Date: " + dt.ToString()); // Datetime in different formats Console.WriteLine("***Datetime in different formats***"); Console.WriteLine(dt.ToString("dd/MM/yyyy")); Console.WriteLine(dt.ToString("dd-MM-yyyy")); Console.WriteLine(dt.ToString("MM/dd/yyyy")); Console.WriteLine(dt.ToString("MM-dd-yyyy")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy")); Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("yyyyMMddHHmmss")); Console.WriteLine(dt.ToString("HH:mm")); Console.WriteLine(dt.ToString("dd/MM/yyyy HH:mm")); Console.WriteLine(dt.ToString("HH:mm:ss")); Console.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss")); Console.WriteLine(dt.ToString("hh:mm tt")); Console.WriteLine(dt.ToString("dd/MM/yyyy hh:mm tt")); Console.WriteLine(dt.ToString("H:mm")); Console.WriteLine(dt.ToString("dd/MM/yyyy H:mm")); Console.WriteLine(dt.ToString("h:mm tt")); Console.WriteLine(dt.ToString("MM/dd/yyyy h:mm tt")); Console.WriteLine(dt.ToString("MMMM dd")); Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss.fffffffK")); Console.WriteLine(dt.ToString("ddd, dd MMM yyy HH:mm:ss 'GMT'")); Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(dt.ToString("yyyy MMMM")); Console.ReadLine(); } } }
The output of the above program will be something like this:

The following table describes various C# DateTime formats and their results. Here we see all the patterns of the C# DateTime format, and the results.
For | Format | Result | Notes |
---|---|---|---|
Day | %d | 7 | Represents the day of the month as a number tom 1 through 31. |
dd | 07 | Represents the day of the month as a number from 01 through 31. | |
ddd | Mon | Represents the abbreviated name of the day (Mon Tues etc.). | |
dddd | Monday | Represents the full name of the day (Monday, Tuesday, etc.) | |
Year | %y | 11 | Represents the year as a number from 1 through 99. |
yy | 11 | Represents the year as a number from 01 through 99. | |
yyyy | 2016 | Represents the year (2011, 2012, etc.) | |
Month | %M | 3 | Represents the Month number (e.g. 3, 4, etc.) |
MM | 03 | Represents the Month number (e.g. 03, 04, etc.) | |
MMM | Mar | Represents the abbreviated Month name (e.g. Mar, Apr, etc.) | |
MMMM | March | Represents the full Month name (e.g. March, April, etc.) | |
Hour (8 PM) | %h | 8 | Represents the 12-hour clock hour (e.g. 8, 9, etc.) |
hh | 08 | Represents the 12-hour clock, with leading 0 (e.g. 08, 09, etc.) | |
HH | 20 | Represents the 24-hour clock hour, with leading 0 (e.g. 22) | |
Minutes | %m | 6 | Minutes |
mm | 06 | Minutes with leading zero | |
Seconds | s | 25 | Seconds |
ss | 25 | Seconds with leading zero | |
AM PM | tt | AM | AM/PM |
TimeZone | zzz | +5:30 | Represents the time zone (e.g. +05.30) |
What Is the Sleep Method?
The sleep method is used to pause the running thread for a specific time span. It accepts time in milliseconds. Sleep is very useful in a multi-threading environment where you want one thread to stop to make way for other threads to complete their execution.
Syntax
System.Threading.Thread.Sleep(1000);
Parsing Strings to DateTime in C#
In many real-world scenarios, dates are received as strings — from APIs, databases, or user input. C# provides multiple ways to safely convert these strings into DateTime
objects.
✅ Using DateTime.Parse
DateTime.Parse()
converts a valid date string into a DateTime
object. It throws an exception if the string is not in a recognized format.
Example – Valid Input
string dateStr = "2025-04-28";
DateTime parsedDate = DateTime.Parse(dateStr);
Console.WriteLine(parsedDate); // Output: 4/28/2025 12:00:00 AM
⚠️ Example – Invalid Input (throws exception)
string invalidDate = "not a date";
DateTime parsed = DateTime.Parse(invalidDate); // Throws FormatException
Use this method only when you’re sure the input is valid, or you’re ready to handle exceptions.
✅ Using DateTime.TryParse
DateTime.TryParse()
is safer. It returns true
if parsing succeeds, and false
if it fails — without throwing an exception.
Example – Safe Parsing
string input = "April 28, 2025";
DateTime result;
bool success = DateTime.TryParse(input, out result);
if (success)
Console.WriteLine(result); // Output: 4/28/2025 12:00:00 AM
else
Console.WriteLine("Invalid date format.");
Example – Invalid Input (fails gracefully)
string badInput = "XYZ date";
bool parsed = DateTime.TryParse(badInput, out DateTime output);
Console.WriteLine(parsed); // Output: False
🌍 Culture Considerations
Parsing is culture-sensitive by default. For consistent results (e.g. on US vs EU systems), specify a culture:
string euDate = "28/04/2025";
var culture = new CultureInfo("en-GB");
DateTime d = DateTime.Parse(euDate, culture);
📝 Summary
Method | Throws Exception? | Usage |
---|---|---|
DateTime.Parse() | Yes (if format invalid) | Use with trusted inputs |
DateTime.TryParse() | No | Safest for user input or external data |
📅 Standard DateTime Format Specifiers (C#)
Format | Pattern | Example Output |
---|---|---|
d | Short date | 4/28/2025 |
D | Long date | Monday, April 28, 2025 |
f | Full date/time (short time) | Monday, April 28, 2025 2:45 PM |
F | Full date/time (long time) | Monday, April 28, 2025 2:45:30 PM |
g | General (short date, short time) | 4/28/2025 2:45 PM |
G | General (short date, long time) | 4/28/2025 2:45:30 PM |
M or m | Month/day | April 28 |
O or o | Round-trip (ISO 8601) | 2025-04-28T14:45:30.0000000 |
R or r | RFC1123 | Mon, 28 Apr 2025 14:45:30 GMT |
s | Sortable (ISO 8601 without timezone) | 2025-04-28T14:45:30 |
t | Short time | 2:45 PM |
T | Long time | 2:45:30 PM |
u | Universal sortable (UTC) | 2025-04-28 14:45:30Z |
U | Universal full date/time (long time) | Monday, April 28, 2025 2:45:30 PM |
Y or y | Year/Month | April 2025 |
🛠️ Custom DateTime Format Specifiers (C#)
Format | Meaning | Example Value |
---|---|---|
yyyy | Four-digit year | 2025 |
yy | Two-digit year | 25 |
MMMM | Full month name | April |
MMM | Abbreviated month name | Apr |
MM | Two-digit month | 04 |
dd | Two-digit day | 28 |
d | Single-digit day | 8 |
HH | 24-hour hour | 14 |
hh | 12-hour hour | 02 |
mm | Minutes | 45 |
ss | Seconds | 30 |
tt | AM/PM designator | PM |
fff | Milliseconds | 123 |
✅ Pro Tip: Combine custom specifiers like "yyyy-MM-dd HH:mm:ss"
for formats like 2025-04-28 14:45:30
.
DateTime Formatting FAQs
What’s the best format for JSON APIs?
Use the round-trip ("o"
) format: DateTime.Now.ToString("o")
→ 2024-03-26T14:30:45.1234567Z
How to format dates for multiple cultures?
Cache CultureInfo
instances for performance:
// German format
var german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(DateTime.Now.ToString("F", german));
Conclusion
In this article, we have learned about the DateTime class. A DateTime object is used to gather information about the date and time of the system or to set a custom date and time for use in a particular application requirement.
Date and Time in C# are handled by DateTime class in C# that provides properties and methods to format dates in different C# DateTime formats.
We also learned about the Sleep method, which is part of the System.Threading and is used to pause the execution for a certain time interval. This allows the programmers to start another thread in a multi-threading environment while the previous thread is paused.