DateTime formats in C#
By FoxLearn 2/7/2025 2:42:35 AM 60
In C#, you can convert a `DateTime` object to a string in various formats using the `ToString()` method. By passing the desired format as a string parameter to the `ToString()` method, you can retrieve the date and time in the required format.
The example below shows how to obtain the date and time as a string in different formats.
// Create a DateTime object for 15th January 2023, 08:45:20:500 var dt = new DateTime(2023, 1, 15, 8, 45, 20, 500); // Various formats Console.WriteLine(dt.ToString("MM/dd/yy")); // 01/15/23 Console.WriteLine(dt.ToString("MM/dd/yyyy")); // 01/15/2023 Console.WriteLine(dt.ToString("dd/MM/yy")); // 15/01/23 Console.WriteLine(dt.ToString("dd-MM-yy")); // 15-01-23 Console.WriteLine(dt.ToString("ddd, dd MMM yyyy")); // Sun, 15 Jan 2023 Console.WriteLine(dt.ToString("dddd, dd MMMM yy")); // Sunday, 15 January 23 Console.WriteLine(dt.ToString("dddd, dd MMMM yyyy HH:mm")); // Sunday, 15 January 2023 08:45 Console.WriteLine(dt.ToString("MM/dd/yy HH:mm")); // 01/15/23 08:45 Console.WriteLine(dt.ToString("MM/dd/yyyy hh:mm tt")); // 01/15/2023 08:45 AM Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm t")); // 01/15/2023 8:45 A Console.WriteLine(dt.ToString("MM/dd/yyyy H:mm:ss")); // 01/15/2023 8:45:20 Console.WriteLine(dt.ToString("MMM dd")); // Jan 15 Console.WriteLine(dt.ToString("MM-dd-yyyyTHH:mm:ss.fff")); // 01-15-2023T08:45:20.500 Console.WriteLine(dt.ToString("MM-dd-yyyy g")); // 01-15-2023 A.D. Console.WriteLine(dt.ToString("HH:mm")); // 08:45 Console.WriteLine(dt.ToString("hh:mm tt")); // 08:45 AM Console.WriteLine(dt.ToString("HH:mm:ss")); // 08:45:20 Console.WriteLine(dt.ToString("'Full DateTime:' MM-dd-yyyyTHH:mm:ss")); // Full DateTime: 01-15-2023T08:45:20
Custom Date Format Specifiers
You can combine one or more of the following format specifiers in the ToString()
method to get the date string in the desired format.
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 showcases all the format specifiers from the table above.
// Create a DateTime object for 12th March 2022, 14:25:50:120 var dt = new DateTime(2022, 3, 12, 14, 25, 50, 120); // Day formats Console.WriteLine("\"d\" -> {0}", dt.ToString("d")); // 3/12/2022 Console.WriteLine("\"d/M/yy\" -> {0}", dt.ToString("d/M/yy")); // 3/12/22 Console.WriteLine("\"dd\" -> {0}", dt.ToString("dd")); // 12 Console.WriteLine("\"ddd\" -> {0}", dt.ToString("ddd")); // Sat Console.WriteLine("\"dddd\" -> {0}", dt.ToString("dddd")); // Saturday // Month formats Console.WriteLine("\"M\" -> {0}", dt.ToString("M")); // 3 Console.WriteLine("\"d/M/yy\" -> {0}", dt.ToString("d/M/yy")); // 3/12/22 Console.WriteLine("\"MM\" -> {0}", dt.ToString("MM")); // 03 Console.WriteLine("\"MMM\" -> {0}", dt.ToString("MMM")); // Mar Console.WriteLine("\"MMMM\" -> {0}", dt.ToString("MMMM")); // March // Year formats Console.WriteLine("\"y\" -> {0}", dt.ToString("y")); // 3/22 Console.WriteLine("\"yy\" -> {0}", dt.ToString("yy")); // 22 Console.WriteLine("\"yyy\" -> {0}", dt.ToString("yyy")); // 2022 Console.WriteLine("\"yyyy\" -> {0}", dt.ToString("yyyy")); // 2022 Console.WriteLine("\"yyyyy\" -> {0}", dt.ToString("yyyyy")); // 02022 // Hour formats Console.WriteLine("\"MM/dd/yy h\" -> {0}", dt.ToString("MM/dd/yy h")); // 03/12/22 2 Console.WriteLine("\"hh\" -> {0}", dt.ToString("hh")); // 02 Console.WriteLine("\"MM/dd/yy H\" -> {0}", dt.ToString("MM/dd/yy H")); // 03/12/22 14 Console.WriteLine("\"HH\" -> {0}", dt.ToString("HH")); // 14 // Minute formats Console.WriteLine("\"m\" -> {0}", dt.ToString("m")); // 25 Console.WriteLine("\"mm\" -> {0}", dt.ToString("mm")); // 25 Console.WriteLine("\"h:m\" -> {0}", dt.ToString("h:m")); // 2:25 Console.WriteLine("\"hh:mm\" -> {0}", dt.ToString("hh:mm")); // 02:25 // Second formats Console.WriteLine("\"s\" -> {0}", dt.ToString("s")); // 2022-03-12T14:25:50 Console.WriteLine("\"ss\" -> {0}", dt.ToString("ss")); // 50 // AM/PM Console.WriteLine("\"hh:mm t\" -> {0}", dt.ToString("hh:mm t")); // 02:25 PM Console.WriteLine("\"hh:mm tt\" -> {0}", dt.ToString("hh:mm tt")); // 02:25 PM // Timezone formats Console.WriteLine("\"MM/dd/yy K\" -> {0}", dt.ToString("MM/dd/yy K")); // 03/12/22 -07:00 Console.WriteLine("\"MM/dd/yy z\" -> {0}", dt.ToString("MM/dd/yy z")); // 03/12/22 -7 Console.WriteLine("\"zz\" -> {0}", dt.ToString("zz")); // -07 Console.WriteLine("\"zzz\" -> {0}", dt.ToString("zzz")); // -07:00
In this example, the DateTime
is set to March 12, 2022, at 14:25:50.120. The various format specifiers are applied to display the date and time in different ways, including day, month, year, hour, minute, second, AM/PM, and timezone formats.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#
Categories
Popular Posts
Stisla Admin Dashboard Template
11/18/2024
Admin Tailwind CSS Admin Dashboard Template
11/18/2024
Portal HTML Bootstrap
11/14/2024