Date Time Format In C#: Let’s Format Your Time

Download Sample Source Code

Get ready to dive into this detailed tutorial where we'll be learning and practicing different Date Time Format In C#.

In this C# Datetime Format tutorial we will discuss various aspects of date time formatting, using the ToString C# format method, various format specifiers, and their detailed explanations like today's date in dd/mm/yyyy format.

KEY TAKEAWAYS:

  1. DateTime is a structure that allows the manipulation of the Date Time Format In C#.
  2. DateTime involves converting it to a string representation in a specific format using ToString() method.
  3. Standard date time format specifiers in C# provide predefined formats based on the conventions of the current culture.
  4. Custom date time format specifiers allow the creation of custom formats for representing date and time.
  5. There are various commonly used custom patterns available for formatting DateTime in C#.

Date Time Format In C#

First, let's understand what DateTime is in C#. DateTime is a structure in C# that allows developers to manipulate date and time in their applications. It's in the System namespace, and it offers various properties and methods for handling date and time.

However, dates and times can be formatted in different ways according to the specific requirements of the application or the locale of the user. That's where c# datetime formatting comes into play. You can use various methods and specifiers to format DateTime according to your needs.

Formatting DateTime C#

Formatting DateTime involves converting a DateTime object to a string that represents a date or time in a specific format. The common method used for this is ToString(), which can be used with various format specifiers to change the format of the date or time.

For instance, to get the current date and time, you can use DateTime.Now and convert this DateTime to a string, using ToString() method, like this:

string date = DateTime.Now.ToString();

This will give you a string with current date and time in the default format. But, in case if you want a different format? That's where the format specifiers come in.

There are several format specifiers in C# under the two types of Date Time formate specifiers, “Standard date time format specifiers, and the Custom date time format specifiers”, that can be used with the DateTime ToString Format method to format date and time. In upcoming sections, you will see a list of most of the specifiers available in C#, what they represent, and how to implement them.

We will organize this article into 3 parts:

  1. Standard Date and Time format strings with examples and results
  2. Custom Date and Time format strings with examples and results
  3. Commonly used custom Patterns for Date Time formats ( You may have a quick bird view of this section first, to get some understanding of what we are going to learn! )

Standard C# Datetime Format

In a standard date and time format string, a single character serves as the format specifier that defines how a DateTime or DateTimeOffset value should be represented in text form.

Standard format strings allow us to represent DateTime values in predefined formats. These formats are based on the conventions of the current culture.

Following is the list of Standard date time format specifiers with implementation using the ToString() method in C#, including the pattern of the date time it will produce.

“Please note that your Local Time Zone and system culture may affect the behavior, pattern, and output of the examples.”

Format Specifier Description Code and Output
d This specifier represents the short date pattern. It displays the date as month, day, and year in numeric form. (MM/dd/yyyy)
DateTime.Now.ToString("d") 
// Result: "5/21/2023"
D This specifier represents the long date pattern. It displays the date as the full date which includes the day of the week, month, and day of the month. (dddd, MMMM dd, yyyy)
DateTime.Now.ToString("D") 
// Result: "Sunday, May 21, 2023"
f This specifier represents the full date and time (long date and short time) pattern. It displays the date as the long date and the time without seconds. (dddd, MMMM dd, yyyy h:mm tt)
DateTime.Now.ToString("f") 
// Result: "Sunday, May 21, 2023 9:30 PM"
F This specifier represents the full date and time (long date and long time) pattern. It displays the date as the long date and the time including seconds. (dddd, MMMM dd, yyyy h:mm:ss tt)
DateTime.Now.ToString("F") 
// Result: 
"Sunday, May 21, 2023 9:30:15 PM"
g This specifier represents the general date and time (short date and short time) pattern. It displays the date as the short date and the time without seconds. (MM/dd/yyyy h:mm tt)
DateTime.Now.ToString("g") 
// Result: "5/21/2023 9:30 PM"
G This specifier represents the general date and time (short date and long time) pattern. It displays the date as the short date and the time including seconds. (MM/dd/yyyy h:mm:ss tt)
DateTime.Now.ToString("G") 
// Result: "5/21/2023 9:30:15 PM"
M and m These specifiers represent the month day pattern. They display the date as the month and day of the month.
DateTime.Now.ToString("M") 
// Result: "May 21"
DateTime.Now.ToString("m") 
// Result: "May 21"
O and o These specifiers represent the round-trip date/time pattern. They display the date and time in a format that can be used for round-tripping, including the time zone offset. (yyyy-MM-ddTHH:mm:ss.fffffffzzz)
DateTime.Now.ToString("O")
DateTime.Now.ToString("o") 
// Result:
"2023-05-21T21:30:15.0000000-07:00"
R and r These specifiers represent the RFC1123 pattern. They display the date and time in the RFC1123 format, which is used in HTTP headers. (ddd, dd MMM yyyy HH:mm:ss 'GMT')
DateTime.Now.ToString("R")
DateTime.Now.ToString("r") 
// Result: "Sun, 21 May 2023 21:30:15 GMT"
s This specifier represents the sortable date/time pattern. It displays the date time in a format that is sortable. (yyyy-MM-ddTHH:mm:ss)
DateTime.Now.ToString("s") 
// Result: "2023-05-21T21:30:15"
t This specifier represents the short time pattern. It displays the time as hours and minutes without seconds. (h:mm tt)
DateTime.Now.ToString("t") 
// Result: "9:30 PM"
T This specifier represents the long time pattern. It displays the time as hours, minutes, and seconds. (h:mm:ss tt)
DateTime.Now.ToString("T") 
// Result: "9:30:15 PM"
u This specifier represents the universal sortable date/time pattern. It displays the date time in a sortable format. (yyyy-MM-dd HH:mm:ss'Z')
DateTime.Now.ToString("u") 
// Result: "2023-05-22 04:30:15Z"
U This specifier represents the full date and time pattern (long date and long time) using the universal time zone. (dddd, MMMM dd, yyyy h:mm:ss tt)
DateTime.Now.ToString("U") 
// Result: 
"Sunday, May 22, 2023 4:30:15 AM"
Y and y These specifiers represent the year month pattern. They display the date as the year and month. (MMMM yyyy)
DateTime.Now.ToString("Y") 
// Result: "May 2023"
DateTime.Now.ToString("y") 
// Result: "May 2023"

C# Datetime Custom Format

If a date and time format string consists of more than one format specifier (character), including white space, it is considered as a custom date and time format string.

Following are different Custom DateTime formates with implementation using ToString() method in C#.

Format Specifier Description Code and Output
d Day of the month, 1 to 31. You have to use this with the combination of other custom format specifiers, otherwise, it will work as standard format and return a short date pattern.
DateTime.Now.ToString("d, MMM")
// Result: 21, May
dd Day of the month with leading zero.
DateTime.Now.ToString("dd")
// Result: 21
ddd Abbreviated weekday name.
DateTime.Now.ToString("ddd")
// Result: Sun
dddd Full weekday name.
DateTime.Now.ToString("dddd")
// Result: Sunday
f Displays seconds fraction to one decimal place. Tenths of a second. You have to use it with combination of other custom specifiers like the example below, otherwise, it will work as a standard specifier and return the full date and time (long date and short time) pattern.
DateTime.Now.ToString("hh:mm:ss.f")
// Result: 12:11:29.1
ff Displays seconds fraction to two decimal places. Hundredths of a second.
DateTime.Now.ToString("ff")
// Result: 
.00 to .99 (depends on the time)
fff Displays seconds fraction to three decimal places. Milliseconds
DateTime.Now.ToString("fff")
// Result: 
.000 to .999 (depends on the time)
ffff Displays seconds fraction to four decimal places. Ten-thousandths of a second.
DateTime.Now.ToString("ffff")
// Result: 
.0000 to .9999 (depends on the time)
fffff Displays seconds fraction to five decimal places. Hundred-thousandths of a second.
DateTime.Now.ToString("fffff")
// Result:
.00000 to .99999 (depends on the time)
ffffff Displays seconds fraction to six decimal places. Millionths of a second.
DateTime.Now.ToString("ffffff")
// Result:
.000000 to .999999 (depends on the time)
fffffff Displays seconds fraction to seven decimal places. Ten-millionths of a second.
DateTime.Now.ToString("fffffff")
// Result:
.0000000 to .9999999 (depends on the time)
g and gg When used with the combination as a custom format specifier, will add the period or era in date time string.
DateTime.Now.ToString("MM/dd/yyyy g")
// Result: 05/21/2023 A.D.
h 12-hour clock hour without leading zero.
DateTime.Now.ToString("h:mm:ss.f")
// Result: 2:29:02.9
hh 12-hour clock hour with leading zero.
DateTime.Now.ToString("hh:mm:ss.f")
// Result: 02:29:02.9
H 24-hour clock hour without leading zero.
DateTime.Now.ToString("H:mm:ss.f")
// Result: 0 to 23 (depends on the time)
HH 24-hour clock hour with leading zero.
DateTime.Now.ToString("HH:mm:ss.f")
// Result:
00 to 23 (depends on the time)
K Time zone information.
DateTime.Now.ToString("%K")    
// Result: +00:00
DateTime.UtcNow.ToString("%K") 
// Result: Z
m Minute without leading zero. Should use with the combination of other specifiers, in case of independent use, it will work as standard format specifier for month day pattern.
DateTime.Now.ToString("h:m:s.F")
// Result: 2:41:8.1
mm Minute with leading zero.
DateTime.Now.ToString("HH:mm:ss.f")
// Result: 02:41:29.9
M Month without leading zero. We have enclosed “M” into braces just for the sack of example, it is not necessary, you can opt this out.
DateTime.Now.ToString("(M) MM MMM MMM")
// Result: (2) 02 Feb February
MM Month with leading zero.
DateTime.Now.ToString("M (MM) MMM MMM")
// Result: 2 (02) Feb (February)
MMM Abbreviated month name.
DateTime.Now.ToString("M MM (MMM) MMM")
// Result: 2 02 (Feb) February
MMMM Full month name.
DateTime.Now.ToString("M MM MMM (MMMM)")
// Result: 2 02 Feb (February)
s Second without leading zero.
DateTime.Now.ToString("h:m:s.F")
// Result: you will see seconds 0 to 59
in place of s (depends on the time)
ss Second with leading zero.
DateTime.Now.ToString("ss")
// Result: 00 to 59 
(depends on the time)
t First character of AM/PM designator. A or P
DateTime.Now.ToString("h:m:s.F t")
// Result: 12:57:22.6 P
tt AM/PM designator.
DateTime.Now.ToString("tt")
// Result: AM or PM
y Year, last two digits (0-99).
DateTime.Now.ToString("%y")
// Result: 23
yy Year, last two digits (00-99).
DateTime.Now.ToString("yy")
// Result: 23
yyy Year with a minimum of three digits.
DateTime.Now.ToString("yyy")
// Result: 2023
yyyy Year with four digits.
DateTime.Now.ToString("yyyy")
// Result: 2023
yyyyy Year with five digits.
DateTime.Now.ToString("yyyyy")
// Result: 02023
z Time zone offset without leading zero.
DateTime.Now.ToString("%z")
// Result: 
-7 to +7 (depends on the timezone)
zz Time zone offset with leading zero.
DateTime.Now.ToString("zz")
// Result: 
-07 to +07 (depends on the timezone)
zzz Full-time zone offset.
DateTime.Now.ToString("zzz")
// Result:
-07:00 to +07:00 (depends on the timezone)

Please note that the output of these format specifiers will vary based on the current date and time.

Commonly used Custom Patterns for Date Time Formats

The code below illustrates different formats for C# Date Time and their corresponding results. It showcases a comprehensive range of patterns for formatting DateTime in C#.

using System;

public class Program
{
    public static void Main()
    {
        DateTime now = DateTime.Now;

        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd")); // Result: "2023/05/21"

        Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy")); // Result: "21/05/2023"

        Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy")); // Result: "05/21/2023"

        Console.WriteLine(DateTime.Now.ToString("MM-dd-yyyy")); // Result: "05-21-2023"

        Console.WriteLine(DateTime.Now.ToString("dd-MM-yyyy")); // Result: "21-05-2023"

        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd")); // Result: "2023-05-21"

        Console.WriteLine(DateTime.Now.ToString("yyyyMMdd")); // Result: "20230521"

        Console.WriteLine(DateTime.Now.ToString("ddMMyyyy")); // Result: "21052023"

        Console.WriteLine(DateTime.Now.ToString("MMddyyyy")); // Result: "05212023"

        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss")); // Result: "17:45:30"

        Console.WriteLine(DateTime.Now.ToString("hh:mm:ss tt")); // Result: "05:45:30 PM"

        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); // Result: "2023/05/21 17:45:30"

        Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt")); // Result: "05/21/2023 05:45:30 PM"

        Console.WriteLine(DateTime.Now.ToString("MMMM dd, yyyy")); // Result: "May 21, 2023"

        Console.WriteLine(DateTime.Now.ToString("MMMM dd, yyyy hh:mm:ss tt")); // Result: "May 21, 2023 05:45:30 PM"

        Console.WriteLine(DateTime.Now.ToString("dddd, MMMM dd, yyyy")); // Result: "Sunday, May 21, 2023"

        Console.WriteLine(DateTime.Now.ToString("ddd, MMM dd, yyyy")); // Result: "Sun, May 21, 2023"

        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")); // Result: "17:45:30.123"

        Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.FFF")); // Result: "17:45:30.123"

        Console.WriteLine(DateTime.Now.ToString("MM-dd-yyyy HH:mm")); // Result: "05-21-2023 17:45"

        Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm tt")); // Result: "2023/05/21 05:45 PM"

        Console.WriteLine(DateTime.Now.ToString("yy/MM/dd")); // Result: "23/05/21"

        Console.WriteLine(DateTime.Now.ToString("dd/MM/yy")); // Result: "21/05/23"

        Console.WriteLine(DateTime.Now.ToString("MM/dd/yy")); // Result: "05/21/23"

        Console.WriteLine(DateTime.Now.ToString("M/d/yyyy")); // Result: "5/21/2023"

        Console.WriteLine(DateTime.Now.ToString("h:mm tt")); // Result: "5:45 PM"
    }
}

Next, we recommend you also read the article about Format String in C#, which we also have on this website for you!