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.
//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/21
mydate.ToString("MM/dd/yyyy");//08/04/2021
mydate.ToString("dd/MM/yy");//04/08/21
mydate.ToString("dd-MM-yy");//04-08-21
mydate.ToString("ddd, dd MMM yyyy"); // Wed, 04 Aug 2021
mydate.ToString("dddd, dd MMMM yy"); // Wednesday, 04 August 21
mydate.ToString("dddd, dd MMMM yyyy HH:mm"); // Wednesday, 04 August 2021 23:58
mydate.ToString("MM/dd/yy HH:mm"); // 08/04/21 23:58
mydate.ToString("MM/dd/yyyy hh:mm tt"); // 08/04/2021 11:58 PM
mydate.ToString("MM/dd/yyyy H:mm t"); // Wed, 04 Aug 2021 P
mydate.ToString("MM/dd/yyyy H:mm:ss"); // 08/04/2021 23:58:30
mydate.ToString("MMM dd"); // Aug 04
mydate.ToString("MM-dd-yyyTHH:mm:ss.fff"); // 08-04-2021T23:58:30.999
mydate.ToString("MM-dd-yyy g"); // 08-04-2021 A.D.
mydate.ToString("HH:mm"); // 23:58
mydate.ToString("hh:mm tt"); // 11:58 PM
mydate.ToString("HH:mm:ss"); // 23:58:30
mydate.ToString("'Full DateTime:' MM-dd-yyyTHH:mm:ss"); // Full DateTime: 08-04-2021T23:58:30
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.
var mydate = new DateTime(2021, 8, 4, 23, 58, 30, 999);
//day formats
Console.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 formats
Console.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 formats
Console.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 formats
Console.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 formats
Console.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 formats
Console.WriteLine("\"s\" -> {0}", mydate.ToString("s"));
Console.WriteLine("\"ss\" -> {0}", mydate.ToString("ss"));
//AM/PM
Console.WriteLine("\"hh:mm t\" -> {0}", mydate.ToString("hh:mm t"));
Console.WriteLine("\"hh:mm tt\" -> {0}", mydate.ToString("hh:mm tt"));
//timezone formats
Console.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"));
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#?