DateTime Formats in C# (2024)

C#

ByTutorialsTeacher

04 Aug 2021

In C#, you can get a date and string from a DateTime object into different formats using the ToString() method. Specify the format as a string parameter in the ToString() method to get the date string in the required format.

The following example demonstrates getting the date and time string in different formats.

Example: DateTime Formats using ToString()

//4th August 2021, 23:58:30:999 (hours:minutes:seconds:milliseconds)var mydate = new DateTime(2021,8,4,23,58,30,999); mydate.ToString("MM/dd/yy"); // 08/4/21mydate.ToString("MM/dd/yyyy");//08/04/2021mydate.ToString("dd/MM/yy");//04/08/21mydate.ToString("dd-MM-yy");//04-08-21mydate.ToString("ddd, dd MMM yyyy"); // Wed, 04 Aug 2021mydate.ToString("dddd, dd MMMM yy"); // Wednesday, 04 August 21mydate.ToString("dddd, dd MMMM yyyy HH:mm"); // Wednesday, 04 August 2021 23:58mydate.ToString("MM/dd/yy HH:mm"); // 08/04/21 23:58mydate.ToString("MM/dd/yyyy hh:mm tt"); // 08/04/2021 11:58 PMmydate.ToString("MM/dd/yyyy H:mm t"); // Wed, 04 Aug 2021 Pmydate.ToString("MM/dd/yyyy H:mm:ss"); // 08/04/2021 23:58:30mydate.ToString("MMM dd"); // Aug 04mydate.ToString("MM-dd-yyyTHH:mm:ss.fff"); // 08-04-2021T23:58:30.999mydate.ToString("MM-dd-yyy g"); // 08-04-2021 A.D.mydate.ToString("HH:mm"); // 23:58mydate.ToString("hh:mm tt"); // 11:58 PMmydate.ToString("HH:mm:ss"); // 23:58:30mydate.ToString("'Full DateTime:' MM-dd-yyyTHH:mm:ss"); // Full DateTime: 08-04-2021T23:58:30

Try it

Custom Date Format Specifiers

You can use a combination of one or more following format specifiers in the ToString() method to get the date string as per your requirement.

Format specifier Description
"d" Represents the single digit day of the month, from 1 through 31.
"dd" Represents the double digits day of the month, from 01 through 31.
"ddd" Represents the abbreviated name of the day of the week.
"dddd" Represents the full name of the day of the week.
"f" Represents the most significant digit of the seconds
"ff" Represents the hundredths of a second in a date and time value.
"fff" Represents the milliseconds in a date and time value.
"ffff" Represents the ten thousandths of a second in a date and time value.
"fffff" Represents the hundred thousandths of a second in a date and time value.
"ffffff" Represents the millionths of a second in a date and time value.
"fffffff" Represents the ten millionths of a second in a date and time value.
"F" Represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero, and the decimal point that follows the number of seconds is also not displayed.
"FF" Represents the hundredths of a second in a date and time value. Trailing zeros aren't displayed. Nothing is displayed if the two significant digits are zero, and in that case, the decimal point that follows the number of seconds is also not displayed.
"FFF" Represents the milliseconds in a date and time value. Trailing zeros aren't displayed. Nothing is displayed if the three significant digits are zero, and in that case the decimal point that follows the number of seconds is also not displayed.
"FFFF" Represents the ten thousandths of a second in a date and time value.
"FFFFF" Represents the hundred thousandths of a second in a date and time value.
"FFFFFF" Represents the millionths of a second in a date and time value.
"FFFFFFF" Represents the ten millionths of a second in a date and time value.
"g", "gg" The period or era: A.D.
"h" Represents the hour, using a 12-hour clock from 1 to 12.
"hh" Represents the double digit hours in 12-hour clock from 01 to 12.
"H" Represents the single digit hours in24-hour clock from 0 to 23.
"HH" Represents the double digit hours in 24-hour clock from 00 to 23.
"K" Represents the time zone information using the DateTime.Kind property.
"m" Represents the minute, from 0 through 59.
"mm" Represents the minute, from 00 through 59.
"M" Represents the month, from 1 through 12.
"MM" Represents the month, from 01 through 12.
"MMM" Represents the abbreviated name of the month.
"MMMM" Represents the full name of the month.
"s" Represents the second, from 0 through 59.
"ss" Represents the double digit seconds, from 00 through 59.
"t" Represents the first character of the AM/PM designator.
"tt" Represents the AM/PM designator.
"y" Represents the year, from 0 to 99.
"yy" Represents the year, from 00 to 99.
"yyy" Represents the year, with a minimum of three digits.
"yyyy" Represents the year as a four-digit number.
"yyyyy" Represents the year as a five-digit number.
"z" Represents Hours offset from UTC, with no leading zeros.
"zz" Represents Hours offset from UTC, with a leading zero for a single-digit value.
"zzz" Represents Hours and minutes offset from UTC.
":" Represents the time separator.
"/" Represents the date separator.
"string" 'string' Represents the literal string delimiter.
% Specifies that the following character as a custom format specifier.
\ Represents the escape character.
Any other character The character is copied to the result string unchanged.

The following example demonstrates all the format specifiers of the above table.

Example: DateTime Formats in C#

var mydate = new DateTime(2021, 8, 4, 23, 58, 30, 999);//day formatsConsole.WriteLine("\"d\" -> {0}", mydate.ToString("d"));Console.WriteLine("\"d/M/yy\" -> {0}", mydate.ToString("d/M/yy"));Console.WriteLine("\"dd\" -> {0}", mydate.ToString("dd"));Console.WriteLine("\"ddd\" -> {0}", mydate.ToString("ddd"));Console.WriteLine("\"dddd\" -> {0}", mydate.ToString("dddd"));//month formatsConsole.WriteLine("\"M\" -> {0}", mydate.ToString("M"));Console.WriteLine("\"d/M/yy\" -> {0}", mydate.ToString("d/M/yy"));Console.WriteLine("\"MM\" -> {0}", mydate.ToString("MM"));Console.WriteLine("\"MMm\" -> {0}", mydate.ToString("MMM"));Console.WriteLine("\"MMMM\" -> {0}", mydate.ToString("MMMM"));//year formatsConsole.WriteLine("\"y\" -> {0}", mydate.ToString("y"));Console.WriteLine("\"yy\" -> {0}", mydate.ToString("yy"));Console.WriteLine("\"yyy\" -> {0}", mydate.ToString("yyy"));Console.WriteLine("\"yyyy\" -> {0}", mydate.ToString("yyyy"));Console.WriteLine("\"yyyyy\" -> {0}", mydate.ToString("yyyyy"));//hour formatsConsole.WriteLine("\"mm/dd/yy h\" -> {0}", mydate.ToString("MM/dd/yy h"));Console.WriteLine("\"hh\" -> {0}", mydate.ToString("hh"));Console.WriteLine("\"mm/dd/yy h\" -> {0}", mydate.ToString("MM/dd/yy H"));Console.WriteLine("\"HH\" -> {0}", mydate.ToString("HH"));//minuts formatsConsole.WriteLine("\"m\" -> {0}", mydate.ToString("m"));Console.WriteLine("\"mm\" -> {0}", mydate.ToString("mm"));Console.WriteLine("\"h:m\" -> {0}", mydate.ToString("h:m"));Console.WriteLine("\"hh:mm\" -> {0}", mydate.ToString("hh:mm"));//second formatsConsole.WriteLine("\"s\" -> {0}", mydate.ToString("s"));Console.WriteLine("\"ss\" -> {0}", mydate.ToString("ss"));//AM/PMConsole.WriteLine("\"hh:mm t\" -> {0}", mydate.ToString("hh:mm t"));Console.WriteLine("\"hh:mm tt\" -> {0}", mydate.ToString("hh:mm tt"));//timezone formatsConsole.WriteLine("\"mm/dd/yy K\" -> {0}", mydate.ToString("MM/dd/yy K"));Console.WriteLine("\"mm/dd/yy z\" -> {0}", mydate.ToString("MM/dd/yy z"));Console.WriteLine("\"zz\" -> {0}", mydate.ToString("zz"));Console.WriteLine("\"zzz\" -> {0}", mydate.ToString("zzz"));

Try it

Learn more about custom date formats here.

Related Articles

  • How to get the sizeof a datatype in C#?
  • Difference between String and StringBuilder in C#
  • Static vs Singleton in C#
  • Difference between == and Equals() Method in C#
  • Asynchronous programming with async, await, Task in C#
  • How to loop through an enum in C#?
  • Generate Random Numbers in C#
  • Difference between Two Dates in C#
  • Convert int to enum in C#
  • BigInteger Data Type in C#
  • Convert String to Enum in C#
  • Convert an Object to JSON in C#
  • Convert JSON String to Object in C#
  • How to convert date object to string in C#?
  • Compare strings in C#
  • How to count elements in C# array?
  • Difference between String and string in C#.
  • How to get a comma separated string from an array in C#?
  • Boxing and Unboxing in C#
  • How to convert string to int in C#?
  • How to calculate the code execution time in C#?

TutorialsTeacher

Author

We are a team of passionate developers, educators, and technology enthusiasts who, with their combined expertise and experience, create in-depth, comprehensive, and easy to understand tutorials. We focus on a blend of theoretical explanations and practical examples to encourages hands-on learning. Learn more about us

  • Tweet
  • Share
  • Whatsapp
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Node.js
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity
DateTime Formats in C# (2024)

FAQs

What is the format of date time in C#? ›

Method: Applying C# DateTime Format yyyy-mm-dd.

What is the alternative to DateTime in C#? ›

You can view the source for the entire set of examples from this article in either Visual Basic, F#, or C#. An alternative to the DateTime structure for working with date and time values in particular time zones is the DateTimeOffset structure.

What is the best DateTime format? ›

Why is the ISO 8601 date format considered the best? The ISO 8601 date format is considered the best because it eliminates ambiguity and confusion. It is an international standard that follows the format “YYYY-MM-DD”. This format is universally understood and is not subject to regional variations.

How to check if DateTime is a specific format in C#? ›

Use the DateTime. TryParseExact method in C# for Date Format validation. They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.

What are the methods of date time in C#? ›

Working with Date and Time in C#
  • DateTime newDate = new DateTime(year, month, day); DateTime newDate = new DateTime(year, month, day, hour, munite, second);
  • DateTime currentDateTime = DateTime.Now; //returns current date and time. ...
  • // Calculate age with TimeSpan struct.
Aug 30, 2020

What is the acceptable date format? ›

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD. So if both Australians and Americans used this, they would both write the date as 2019-02-03.

What is the difference between date and DateTime in C#? ›

DateTime represents some point in time that is composed of a date and a time. However, you can retrieve the date part via the Date property (which is another DateTime with the time set to 00:00:00 ). And you can retrieve individual date properties via Day , Month and Year .

What is the difference between DateTime and TimeSpan in C#? ›

The DateTime and TimeSpan value types differ in that a DateTime represents an instant in time, whereas a TimeSpan represents a time interval. This means, for example, that you can subtract one instance of DateTime from another to obtain the time interval between them.

Why do we use DateTime in C#? ›

DateTime is a struct in C# that represents an instant in time, typically expressed as a date and time of day. It is available in the System namespace and has various constructors, fields, properties, methods, and operators to work with dates and times.

What is the most readable datetime format? ›

The ISO 8601 notation is today the commonly recommended format of representing date and time as human-readable strings in new plain-text communication protocols and file formats.

Which date format is better? ›

In America, the date is formally written in month/day/year form. Thus, “January 1, 2011” is widely considered to be correct. In formal usage, it is not appropriate to omit the year, or to use a purely numerical form of the date.

Which date format is easiest to read? ›

In most cases, writing the date in full letters would be better than the example above. Apr. 3rd, 2002 , for example will be easy to understand for any English-speaking audience.

What is the default DateTime format in C#? ›

C# DateTime Format
FormatResult
DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss")2015-05-16T05:50:06
DateTime.Now.ToString("HH:mm")05:50
DateTime.Now.ToString("hh:mm tt")05:50 AM
DateTime.Now.ToString("H:mm")5:50
18 more rows
Oct 4, 2023

Can we compare DateTime in C#? ›

C# provides several methods and operators for comparing DateTime values, including the Equals() method, the Compare() method, the CompareTo() method, and the various comparison operators. The Equals method checks if two DateTime values are equal.

How to create a specific DateTime in C#? ›

DateTime DT = new DateTime(2019,05,09,9,15,0);// this will initialize variable with a specific date(09/05/2019) and time(9:15:00). Note: The (year, month, day, hour, minute, second) format is followed.

What is the DateTime format? ›

dd/MM/yyyy — Example: 23/06/2013. yyyy/M/d — Example: 2013/6/23. yyyy-MM-dd — Example: 2013-06-23. yyyyMMddTHH:mmzzz — Example: 20130623T13:22-0500.

How to get mm dd yyyy format in C#? ›

If you have to put your DateTime value directly into your query string, you can simply use myDate. ToString("MM/dd/yyyy",CultureInfo. InvariantCulture); .

What is the format of a timestamp? ›

With the fractional part included, the format for these values is ' YYYY-MM-DD hh:mm:ss [. fraction ]' , the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.499999' , and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.499999' .

How to create date time in C#? ›

There are two ways to initialize the DateTime variable: DateTime DT = new DateTime();// this will initialze variable with a date(01/01/0001) and time(00:00:00). DateTime DT = new DateTime(2019,05,09,9,15,0);// this will initialize variable with a specific date(09/05/2019) and time(9:15:00).

Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6786

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.