{"id":5907,"date":"2025-06-16T01:02:48","date_gmt":"2025-06-16T01:02:48","guid":{"rendered":"https:\/\/blog.ndepend.com\/?p=5907"},"modified":"2025-07-14T10:22:38","modified_gmt":"2025-07-14T10:22:38","slug":"csharp-datetime-format","status":"publish","type":"post","link":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/","title":{"rendered":"C# DateTime Format In A Nutshell"},"content":{"rendered":"<span>June 16, 2025<\/span> <span style=\"width:22px; display: inline-block;\"> <\/span> <span>8 minutes read <\/span><p><a href=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5993\" src=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png\" alt=\"C# DateTime Format In A-Nutshell\" width=\"1254\" height=\"486\" srcset=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png 1254w, https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1-300x116.png 300w, https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1-1024x397.png 1024w, https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1-768x298.png 768w\" sizes=\"auto, (max-width: 1254px) 100vw, 1254px\" \/><\/a><\/p>\n<p>This article covers <strong>C# DateTime Format<\/strong>, addressing both of its main aspects:<\/p>\n<ul>\n<li><strong>Obtain a date-formatted string.<\/strong>\u00a0Use a pattern like <code>\"yyyy-MM-dd HH:mm:ss\"<\/code> to extract and format the year, month, day, hour, minute, and second from a <code>DateTime<\/code> instance into a string like <code>\"2025-04-25 17:07:17\"<\/code>.<\/li>\n<li><strong>Parse a date formatted string <\/strong>to obtain back a <code>DateTime<\/code>\u00a0instance.<\/li>\n<\/ul>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Index<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#DateTime_Display_Formats\" >DateTime Display Formats<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#Key_Format_Specifiers\" >Key Format Specifiers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#Character_Literals\" >Character Literals<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#Standard_Format_Specifiers\" >Standard Format Specifiers<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#The_Role_of_the_Culture_in_DateTime_Formatting\" >The Role of the Culture in DateTime Formatting<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#Parsing_a_Formatted_DateTime_String\" >Parsing a Formatted DateTime String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#Formatting_DateTimeOffset_and_TimeSpan\" >Formatting DateTimeOffset and TimeSpan<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#DateTimeOffset_DateTime_with_Time_Zone_Awareness\" >DateTimeOffset: DateTime with Time Zone Awareness<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#TimeSpan_Representing_Durations\" >TimeSpan: Representing Durations<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#DateTime_Formatting_in_C_Best_Practices\" >DateTime Formatting in C#: Best Practices<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"DateTime_Display_Formats\"><\/span>DateTime Display Formats<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>readonly struct System.DateTime<\/code> defined in the .NET Base Class Library (BCL) is the key to handling dates and times in C#. You can experiment with various formats with this short C# program:<\/p>\n<pre class=\"lang:c decode:true\">\/\/var dateTime = DateTime.Now; uncomment to get the current date and time\r\n\/\/ or use this fixed date-time: 25 April 2025 05:07:08.730\r\nvar dateTime = new DateTime(2025, 4, 25, 5, 7, 8, 730, DateTimeKind.Local); \r\nConsole.WriteLine(dateTime.ToString(\"ddd, dd MMM yyyy h:mm\"));\r\n<\/pre>\n<p>The table below shows various formats and their outputs, allowing flexible customization for dates, times, or timezones.<\/p>\n<style>\n.cell {        padding: 5px 3px;    }    .highlight {        background: #f1f1f1;    }<\/style>\n<table style=\"width: 100%; overflow: hidden;\">\n<thead>\n<tr>\n<th style=\"width: 30%; padding: 5px 3px;\"><code>dateTime.ToString(\"\u2026\")<\/code><\/th>\n<th style=\"width: 30%; padding: 5px 3px;\">Result String<\/th>\n<th style=\"width: 40%; padding: 5px 3px;\">Remarks<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"cell\"><code>\"dd\/MM\/yyyy\"<\/code><\/td>\n<td class=\"cell\"><strong>25-04-2025<\/strong><\/td>\n<td class=\"cell\"><code>\/<\/code> the system date separator<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"yyyy MMMM\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>2025 April<\/strong><\/td>\n<td class=\"cell highlight\">MMMM name of the month<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"MMM d\"<\/code><\/td>\n<td class=\"cell\"><strong>Apr 25<\/strong><\/td>\n<td class=\"cell\">\u00a0<code>MMM<\/code> abbreviated name of the month<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"ddd, dd MMM yy\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Thu, 25 Apr 24<\/strong><\/td>\n<td class=\"cell highlight\"><code>yy<\/code>\u00a0the year, from 00 to 99.<br \/>\n<code>ddd<\/code> abbreviated name of the day<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"dddd, dd MMMM yyyy\"<\/code><\/td>\n<td class=\"cell\"><strong>Friday, 25 April 2025<\/strong><\/td>\n<td class=\"cell\"><code>dddd<\/code> name of the day<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"ddd, dd MMM yyyy h:mm\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Thu, 25 Apr 2025 5:07<\/strong><\/td>\n<td class=\"cell highlight\">date and time together<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"yyyy-MM-dd HH:mm:ss\"<\/code><\/td>\n<td class=\"cell\"><strong>2025-04-25 17:07:17<\/strong><\/td>\n<td class=\"cell\">Format that can be sorted<br \/>\n<code>:<\/code> the system time separator<br \/>\n<code>HH<\/code> hour from <code>00<\/code> to <code>23<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"hh:mm:ss tt\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>05:07:06 PM<\/strong><\/td>\n<td class=\"cell highlight\"><code>tt<\/code> the AM\/PM designator.<br \/>\n<code>hh<\/code> hour from <code>00<\/code> to <code>11<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"h:m:s t\"<\/code><\/td>\n<td class=\"cell\"><strong>5:7:6 P<\/strong><\/td>\n<td class=\"cell\"><code>t<\/code> the A\/M designator.<br \/>\nNo leading <code>0<\/code> with <code>h m s<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"H:mm:ss\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>17:07:06<\/strong><\/td>\n<td class=\"cell highlight\"><code>H<\/code> hour from <code>0<\/code> to <code>23<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"HH:mm:ss.ff\"<\/code><\/td>\n<td class=\"cell\"><strong>17:07:06.73<\/strong><\/td>\n<td class=\"cell\"><code>f<\/code> for a <b>f<\/b>raction of a second<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"HH:mm:ss.fffffff\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>17:07:06.7301481<\/strong><\/td>\n<td class=\"cell highlight\">use as many <code>f<\/code> as needed<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"HH:mm:ss UTC\"<\/code><\/td>\n<td class=\"cell\"><strong>17:07:06 UTC<\/strong><\/td>\n<td class=\"cell\"><code>UTC<\/code> chars are not interpreted<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"hh:mm:ss K\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>05:07:06 +04:00<\/strong><\/td>\n<td class=\"cell highlight\">\u00a0<code>K<\/code> time zone information.<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"hh:mm:ss z\"<\/code><\/td>\n<td class=\"cell\"><strong>05:07:06 +4<\/strong><\/td>\n<td class=\"cell\">\u00a0<code>z<\/code> time zone information from UTC.<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"hh:mm:ss zz\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>05:07:06 +04<\/strong><\/td>\n<td class=\"cell highlight\">\u00a0<code>zz<\/code> time zone information from UTC<br \/>\nwith leading zero<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"HH:\\\\mm\"<\/code><\/td>\n<td class=\"cell\"><strong>17:m7<\/strong><\/td>\n<td class=\"cell\"><code>\\<\/code> is the escape character<br \/>\nneed <code>\\\\<\/code> in a C# literal string<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>@\"HH:\\mm\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>17:m7<\/strong><\/td>\n<td class=\"cell highlight\"><code>\\<\/code> escape character, don&#8217;t need<br \/>\n<code>\\\\<\/code> in a C# verbatim literal string <code>@\"\u2026\"<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"HH:'mm':ss\"<\/code><\/td>\n<td class=\"cell\"><strong>17:mm:06<\/strong><\/td>\n<td class=\"cell\">chars in <code>'\u2026'<\/code> are escaped<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"HH:\\\"mm\\\":ss\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>17:mm:06<\/strong><\/td>\n<td class=\"cell highlight\">chars in <code>\"\u2026\"<\/code> are escaped<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><span class=\"ez-toc-section\" id=\"Key_Format_Specifiers\"><\/span>Key Format Specifiers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here are brief explanations of some commonly used format specifiers:<\/p>\n<ul>\n<li><strong>d<\/strong>: Day of the month as a number from 1 through 31.<\/li>\n<li><strong>dd<\/strong>: Day of the month as a number from 01 through 31.<\/li>\n<li><strong>ddd<\/strong>: Abbreviated name of the day (Mon, Tue, etc.).<\/li>\n<li><strong>dddd<\/strong>: Full name of the day (Monday, Tuesday, etc.).<\/li>\n<li><strong>h<\/strong>: 12-hour clock hour (e.g., 4).<\/li>\n<li><strong>hh<\/strong>: 12-hour clock, with a leading 0 (e.g., 06).<\/li>\n<li><strong>H<\/strong>: 24-hour clock hour (e.g., 15).<\/li>\n<li><strong>HH<\/strong>: 24-hour clock hour, with a leading 0 (e.g., 22).<\/li>\n<li><strong>m<\/strong>: Minutes.<\/li>\n<li><strong>mm<\/strong>: Minutes with a leading zero.<\/li>\n<li><strong>M<\/strong>: Month number (e.g., 3).<\/li>\n<li><strong>MM<\/strong>: Month number with a leading zero (e.g., 04).<\/li>\n<li><strong>MMM<\/strong>: Abbreviated month name (e.g., Dec).<\/li>\n<li><strong>MMMM<\/strong>: Full month name (e.g., December).<\/li>\n<li><strong>s<\/strong>: Seconds.<\/li>\n<li><strong>ss<\/strong>: Seconds with a leading zero.<\/li>\n<li><strong>f<\/strong>: Represents the leading digit of the seconds fraction, specifically the tenths of a second, in a date and time value.<\/li>\n<li><strong>ff<\/strong>: Represents the two leading digits of the fraction of a second in a date and time value.<\/li>\n<li><strong>t<\/strong>: Abbreviated AM\/PM (e.g., A or P).<\/li>\n<li><strong>tt<\/strong>: AM\/PM (e.g., AM or PM).<\/li>\n<li><strong>yyyy<\/strong>: Year (e.g., 2015).<\/li>\n<li><strong>z<\/strong>: Hours offset from UTC, without leading zeros (e.g., +4).<\/li>\n<li><strong>zz<\/strong>: Hours offset from UTC, with a leading zero if it&#8217;s a single-digit hour. (e.g., +04).<\/li>\n<li><strong>zzz<\/strong>: Hours and minutes offset from UTC (e.g., +04:00).<\/li>\n<li><strong>K<\/strong>: Time zone information of a date and time value (e.g., +04:00). Unlike <strong>zzz<\/strong>,\u00a0<strong>K<\/strong> can adapt its output based on the kind of time being represented (local, UTC, or unspecified), potentially displaying nothing.<\/li>\n<\/ul>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h3 id=\"Literals\" class=\"heading-anchor\"><span class=\"ez-toc-section\" id=\"Character_Literals\"><\/span>Character Literals<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/div>\n<p>The characters <code>FHKMdfghmstyz%:\/\\\"'<\/code> are reserved in a date and time format string. They are interpreted as formatting characters except when these special characters are used: <code>\"<\/code>,\u00a0<code>'<\/code>, and <code>\\<\/code> like in:<\/p>\n<ul>\n<li><code>\"dddd, d MMM, yyyy \\\"at\\\" HH:mm\"<\/code><\/li>\n<li><code>\"dddd, d MMM, yyyy 'at' HH:mm\"<\/code><\/li>\n<li><code>\"dddd, d MMM, yyyy \\a\\t HH:mm\"<\/code><\/li>\n<\/ul>\n<p>All other characters are consistently interpreted as character literals and remain unchanged in the output during a formatting operation.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Standard_Format_Specifiers\"><\/span>Standard Format Specifiers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In addition to key format specifiers, there are some standard ones, each represented by a single letter. Here there are:<\/p>\n<table style=\"width: 100%; overflow: hidden;\">\n<thead>\n<tr>\n<th style=\"width: 20%; padding: 5px 3px;\"><code>dateTime.ToString(\"\u2026\")<\/code><\/th>\n<th style=\"width: 40%; padding: 5px 3px;\">Result String<\/th>\n<th style=\"width: 40%; padding: 5px 3px;\">Remarks (*) defined for each culture<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"cell\"><code>\"d\"<\/code><\/td>\n<td class=\"cell\"><strong>25-Apr-24<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#ShortDate\" target=\"_blank\" rel=\"noopener\">Short date pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"D\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Friday, April 25, 2025<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#LongDate\" target=\"_blank\" rel=\"noopener\">Long date pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"f\"<\/code><\/td>\n<td class=\"cell\"><strong>Friday, April 25, 2025 5:07 AM<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#the-full-date-short-time-f-format-specifier\" target=\"_blank\" rel=\"noopener\">Full date short time<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"F\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Friday, April 25, 2025 5:07:08 AM<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#the-full-date-long-time-f-format-specifier\" target=\"_blank\" rel=\"noopener\">Full date long time<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"g\"<\/code><\/td>\n<td class=\"cell\"><strong>25-Apr-24 5:07 AM<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#GeneralDateShortTime\" target=\"_blank\" rel=\"noopener\">General date short time<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"G\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>25-Apr-24 5:07:08 AM<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#GeneralDateLongTime\" target=\"_blank\" rel=\"noopener\">General date long time<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"m\"<\/code> or <code>\"M\"<\/code><\/td>\n<td class=\"cell\"><strong>April 25<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#MonthDay\" target=\"_blank\" rel=\"noopener\">Month\/day pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"o\"<\/code> or <code>\"O\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>2025-04-25T05:07:08.7300000+04:00<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#Roundtrip\" target=\"_blank\" rel=\"noopener\">Round-trip date\/time pattern<\/a> (ISO 8601)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"s\"<\/code><\/td>\n<td class=\"cell\"><strong>2025-04-25T05:07:08<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#Sortable\" target=\"_blank\" rel=\"noopener\">Sortable date\/time pattern<\/a>\u00a0(ISO 8601)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"r\"<\/code> or <code>\"R\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Thu, 25 Apr 2025 05:07:08 GMT<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#RFC1123\" target=\"_blank\" rel=\"noopener\">RFC1123 pattern<\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"t\"<\/code><\/td>\n<td class=\"cell\"><strong>5:07 AM<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#ShortTime\" target=\"_blank\" rel=\"noopener\">Short time pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"T\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>5:07:08 AM<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#LongTime\" target=\"_blank\" rel=\"noopener\">Long time pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"u\"<\/code><\/td>\n<td class=\"cell\"><strong>2025-04-25 05:07:08Z<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#UniversalSortable\" target=\"_blank\" rel=\"noopener\">Universal sortable date\/time pattern<\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\"><code>\"U\"<\/code><\/td>\n<td class=\"cell highlight\"><strong>Friday, April 25, 2025 1:07:08 AM<\/strong><\/td>\n<td class=\"cell highlight\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#UniversalFull\" target=\"_blank\" rel=\"noopener\">Universal full date\/time pattern<\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"cell\"><code>\"y\"<\/code> or <code>\"Y\"<\/code><\/td>\n<td class=\"cell\"><strong>April 2025<\/strong><\/td>\n<td class=\"cell\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-date-and-time-format-strings#YearMonth\" target=\"_blank\" rel=\"noopener\">Year month pattern<\/a> (*)<\/td>\n<\/tr>\n<tr>\n<td class=\"cell highlight\">Any other single character<\/td>\n<td class=\"cell highlight\"><\/td>\n<td class=\"cell highlight\">Throws a <code>FormatException<\/code> at runtime<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"The_Role_of_the_Culture_in_DateTime_Formatting\"><\/span>The Role of the Culture in DateTime Formatting<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Up to this point, all our experiments of displaying <code>DateTime<\/code> values have been relying on the English (United States) culture. Actually, the current thread&#8217;s culture determines how a date is formatted. Below is a sample program that illustrates this concept:<\/p>\n<pre class=\"lang:c decode:true\">var dateTime = new DateTime(2025, 4, 25, 5, 7, 8, 730, DateTimeKind.Local);\r\n\r\nvar enUs = new CultureInfo(\"en-US\");\r\nvar frFR = new CultureInfo(\"fr-FR\");\r\n\r\n\/\/ Modify the current thread culture\r\nThread.CurrentThread.CurrentCulture = frFR; \r\n\r\n\/\/ print:   vendredi, 25 avril 2025  because the current thread culture is used\r\nConsole.WriteLine(dateTime.ToString(\"dddd, dd MMMM yyyy\"));\r\n\r\n\/\/ print:   Friday, 25 April 2025  because we explicitly specified the US culture\r\nConsole.WriteLine(dateTime.ToString(\"dddd, dd MMMM yyyy\", enUs));<\/pre>\n<p>Also, each culture has its own pattern for formatting a <code>DateTime<\/code>. This is demonstrated by this program:<\/p>\n<pre class=\"lang:c decode:true\">\/\/ Print: \"M\/d\/yyyy\"     4\/25\/2025\r\nConsole.WriteLine(new CultureInfo(\"en-US\").DateTimeFormat.ShortDatePattern);\r\n\r\n\/\/ Print: \"dd\/MM\/yyyy\"  25\/04\/2025\r\nConsole.WriteLine(new CultureInfo(\"fr-FR\").DateTimeFormat.ShortDatePattern);\r\n\r\n\/\/ Print: \"yyyy\/MM\/dd\"  2025\/04\/25\r\nConsole.WriteLine(new CultureInfo(\"ja-JP\").DateTimeFormat.ShortDatePattern);<\/pre>\n<p>As a reminder ????<\/p>\n<p><a href=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/A-map-of-all-countries-that-use-the-MMDDYYYY-date-format.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5914\" src=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/A-map-of-all-countries-that-use-the-MMDDYYYY-date-format.jpg\" alt=\"A map of all countries that use the MMDDYYYY date format\" width=\"743\" height=\"478\" srcset=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/A-map-of-all-countries-that-use-the-MMDDYYYY-date-format.jpg 743w, https:\/\/blog.ndepend.com\/wp-content\/uploads\/A-map-of-all-countries-that-use-the-MMDDYYYY-date-format-300x193.jpg 300w\" sizes=\"auto, (max-width: 743px) 100vw, 743px\" \/><\/a><\/p>\n<p>It may seem acceptable to overlook cultural settings when working with <code>DateTime<\/code> formats that aren&#8217;t being parsed from strings. However, as a professional developer, you undoubtedly recognize the necessity of parsing dates from strings at some point in your work.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Parsing_a_Formatted_DateTime_String\"><\/span>Parsing a Formatted DateTime String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"w-full text-zinc-800 text-lg my-8 font-normal leading-relaxed\">The methods <code>DateTime.ParseExact()<\/code> or <code>DateTime.TryParseExact()<\/code>, will be your friends when parsing a <code>DateTime<\/code>. The following sample program showcases its use, carefully considering cultural nuances and time zone information:<\/p>\n<pre class=\"lang:c decode:true\">var dateTime = new DateTime(2025, 4, 25, 5, 7, 8, 730, DateTimeKind.Local);\r\n\r\nvar enUs = new CultureInfo(\"en-us\");\r\nvar frFR = new CultureInfo(\"fr-fr\");\r\n\r\nThread.CurrentThread.CurrentCulture = frFR;\r\n\r\nconst string FORMAT = \"dddd, dd MMMM yyyy  HH:mm:ss.fff K\";\r\n\r\n\/\/ dateFormatted is   \"vendredi, 25 avril 2025  05:07:08.730 +04:00\"\r\nstring dateFormatted = dateTime.ToString(FORMAT);\r\n\r\n\/\/ parse dateFormatted knowing it has been formatted with the frFR culture\r\nDateTime dateTimeParsed = DateTime.ParseExact(dateFormatted, FORMAT, frFR);\r\n\r\nAssert.IsTrue(dateTime == dateTimeParsed);\r\n\r\n\/\/ print:   \"Friday, 25 April 2025  05:07:08.730 +04:00\"\r\n\/\/ because we explicitly specified the enUS culture\r\nConsole.WriteLine(dateTimeParsed.ToString(FORMAT, enUs));<\/pre>\n<p>You can still try to parse a date time from a string without specifying a format and a culture but this would make your code very fragile. This can lead to bugs that are hard to detect, especially in environments where the application might be running under different cultural settings.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Formatting_DateTimeOffset_and_TimeSpan\"><\/span>Formatting DateTimeOffset and TimeSpan<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While <code data-start=\"229\" data-end=\"239\">DateTime<\/code> is the most commonly used type for handling date and time values in C#, two other important structures \u2014 <code data-start=\"345\" data-end=\"361\">DateTimeOffset<\/code> and <code data-start=\"366\" data-end=\"376\">TimeSpan<\/code> \u2014 play key roles when dealing with time zones and durations.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"DateTimeOffset_DateTime_with_Time_Zone_Awareness\"><\/span>DateTimeOffset: DateTime with Time Zone Awareness<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <code data-start=\"501\" data-end=\"517\">DateTimeOffset<\/code> type represents a point in time relative to Coordinated Universal Time (UTC), combining a <code data-start=\"608\" data-end=\"618\">DateTime<\/code> value with an offset. This makes it ideal for applications that require precise tracking of dates and times across different time zones.<\/p>\n<pre class=\"lang:c decode:true\">DateTimeOffset dto = DateTimeOffset.Now;\r\nConsole.WriteLine(dto); \/\/ e.g., 2025-04-25 15:30:00 +02:00\r\n<\/pre>\n<p>You can format a <code data-start=\"904\" data-end=\"920\">DateTimeOffset<\/code> using standard or custom date\/time format strings, just like <code data-start=\"982\" data-end=\"992\">DateTime<\/code>:<\/p>\n<pre class=\"lang:c decode:true\">string formatted = dto.ToString(\"yyyy-MM-dd HH:mm zzz\");\r\n\/\/ Output: 2025-04-25 15:30 +02:00\r\n<\/pre>\n<p>Using <code data-start=\"904\" data-end=\"920\">DateTimeOffset<\/code> is recommended when you&#8217;re storing or transferring date and time values that must retain the original time zone context.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"TimeSpan_Representing_Durations\"><\/span>TimeSpan: Representing Durations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The TimeSpan structure represents a time interval \u2014 a difference between two points in time \u2014 rather than a specific date or time.<\/p>\n<pre class=\"lang:c decode:true \">TimeSpan duration = new TimeSpan(1, 30, 0); \/\/ 1 hour 30 minutes\r\nConsole.WriteLine(duration); \/\/ Output: 01:30:00\r\n<\/pre>\n<p>You can also calculate a <code data-start=\"1590\" data-end=\"1600\">TimeSpan<\/code> by subtracting two <code data-start=\"1620\" data-end=\"1630\">DateTime<\/code> or <code data-start=\"1634\" data-end=\"1650\">DateTimeOffset<\/code> values:<\/p>\n<pre class=\"lang:c decode:true \">DateTime start = DateTime.Now;\r\nDateTime end = start.AddHours(2.5);\r\nTimeSpan diff = end - start;\r\nConsole.WriteLine(diff.TotalMinutes); \/\/ Output: 150\r\n<\/pre>\n<p>Formatting a <code data-start=\"1837\" data-end=\"1847\">TimeSpan<\/code> is straightforward using <code data-start=\"1873\" data-end=\"1886\">.ToString()<\/code> or custom format strings:<\/p>\n<pre class=\"lang:c decode:true \">string formattedSpan = diff.ToString(@\"hh\\:mm\\:ss\"); \/\/ Output: 02:30:00\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"DateTime_Formatting_in_C_Best_Practices\"><\/span>DateTime Formatting in C#: Best Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The main advice is: <strong>Don&#8217;t use a number for a month because it leads to confusion!<\/strong> Which date is <code>\"05 04 2025\"<\/code>? You can use instead <code>\"dd MMM yyyy\"<\/code>\u00a0that leads to <code>\"05 Apr 2025\"<\/code>. But with such an <strong>unambiguous format<\/strong>, we have to take account of the culture because of the month&#8217;s abbreviated name.<\/p>\n<p>Other common best practices are:<\/p>\n<ul>\n<li><strong>Consistency is Key<\/strong>: Always use a consistent <code>DateTime<\/code> format across your application unless the context specifically demands variation, like the need to sort formatted dates.<\/li>\n<li><strong>Localization Matters<\/strong>: Consider the local date and time formats of your application&#8217;s users. Localization methods shown in this post help render <code>DateTime<\/code> in a culturally appropriate manner.<\/li>\n<li><strong>Sorting Date Might Matter:<\/strong> If formatted dates need to be sorted, this popular format needs to be used: <code>yyyy-MM-dd<\/code><\/li>\n<li><strong>Performance<\/strong>: Note that parsing and formatting <code>DateTime<\/code> is a computationally expensive operation. Format dates only when necessary. <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-8\/#datetime\" target=\"_blank\" rel=\"noopener\">.NET 8 improved DateTime formatting performance<\/a>. However, if performance is a priority and the format is hardcoded, you can develop your own highly efficient formatting and parsing code, similar to what we did in <a href=\"https:\/\/www.ndepend.com\/\" target=\"_blank\" rel=\"noopener\">NDepend<\/a>. If you choose this route, tools like ChatGPT or Copilot can help by generating much of the necessary code for you.<\/li>\n<li><strong>NodaTime for Advanced Scenario<\/strong>: The .NET community is fortunate to have access to <a href=\"https:\/\/nodatime.org\/\" target=\"_blank\" rel=\"noopener\">NodaTime<\/a>, a library developed by recognized expert and writer <a href=\"https:\/\/codeblog.jonskeet.uk\/\" target=\"_blank\" rel=\"noopener\">Jon Skeet<\/a> from the Java library <a href=\"https:\/\/www.joda.org\/joda-time\/\" target=\"_blank\" rel=\"noopener\">Joda Time<\/a>. If date and time are central to your application, NodaTime is worth exploring.<\/li>\n<\/ul>\n<p>By mastering <code>DateTime<\/code> formatting, you can ensure that your applications handle date and time data efficiently and effectively, tailored to the needs of various use cases and locales. Let&#8217;s conclude with this meme that always brings a smile to my face ????<\/p>\n<p><a href=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/DateTime-Format-Humour.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5975\" src=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/DateTime-Format-Humour.jpg\" alt=\"DateTime Format Humour\" width=\"700\" height=\"689\" srcset=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/DateTime-Format-Humour.jpg 700w, https:\/\/blog.ndepend.com\/wp-content\/uploads\/DateTime-Format-Humour-300x295.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>June 16, 2025 8 minutes read This article covers C# DateTime Format, addressing both of its main aspects: Obtain a date-formatted string.\u00a0Use a pattern like &#8220;yyyy-MM-dd HH:mm:ss&#8221; to extract and format the year, month, day, hour, minute, and second from a DateTime instance into a string like &#8220;2025-04-25 17:07:17&#8221;. Parse a date formatted string to &hellip; <a href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C# DateTime Format In A Nutshell<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[77,62],"tags":[],"class_list":["post-5907","post","type-post","status-publish","format-standard","hentry","category-net","category-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C# DateTime Format In A Nutshell - NDepend Blog<\/title>\n<meta name=\"description\" content=\"Use a pattern like &quot;yyyy-MM-dd HH:mm:ss&quot; to extract and format the year, month, day, hour, minute, and second from a DateTime into a string\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# DateTime Format In A Nutshell - NDepend Blog\" \/>\n<meta property=\"og:description\" content=\"Use a pattern like &quot;yyyy-MM-dd HH:mm:ss&quot; to extract and format the year, month, day, hour, minute, and second from a DateTime into a string\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.ndepend.com\/csharp-datetime-format\/\" \/>\n<meta property=\"og:site_name\" content=\"NDepend Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-16T01:02:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-14T10:22:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1254\" \/>\n\t<meta property=\"og:image:height\" content=\"486\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"NDepend\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"NDepend\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/\"},\"author\":{\"name\":\"NDepend\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/#\\\/schema\\\/person\\\/9991dbed4e784e5445b8552129506656\"},\"headline\":\"C# DateTime Format In A Nutshell\",\"datePublished\":\"2025-06-16T01:02:48+00:00\",\"dateModified\":\"2025-07-14T10:22:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/\"},\"wordCount\":1305,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.ndepend.com\\\/wp-content\\\/uploads\\\/C-DateTime-Format-In-A-Nutshell-1.png\",\"articleSection\":[\".NET\",\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/\",\"url\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/\",\"name\":\"C# DateTime Format In A Nutshell - NDepend Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.ndepend.com\\\/wp-content\\\/uploads\\\/C-DateTime-Format-In-A-Nutshell-1.png\",\"datePublished\":\"2025-06-16T01:02:48+00:00\",\"dateModified\":\"2025-07-14T10:22:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/#\\\/schema\\\/person\\\/9991dbed4e784e5445b8552129506656\"},\"description\":\"Use a pattern like \\\"yyyy-MM-dd HH:mm:ss\\\" to extract and format the year, month, day, hour, minute, and second from a DateTime into a string\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blog.ndepend.com\\\/wp-content\\\/uploads\\\/C-DateTime-Format-In-A-Nutshell-1.png\",\"contentUrl\":\"https:\\\/\\\/blog.ndepend.com\\\/wp-content\\\/uploads\\\/C-DateTime-Format-In-A-Nutshell-1.png\",\"width\":1254,\"height\":486,\"caption\":\"C# DateTime Format In A-Nutshell\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/csharp-datetime-format\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.ndepend.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# DateTime Format In A Nutshell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/#website\",\"url\":\"https:\\\/\\\/blog.ndepend.com\\\/\",\"name\":\"NDepend Blog\",\"description\":\"Improve your .NET code quality with NDepend\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.ndepend.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.ndepend.com\\\/#\\\/schema\\\/person\\\/9991dbed4e784e5445b8552129506656\",\"name\":\"NDepend\",\"description\":\"Do you like reading our blog posts about advanced .NET and C# topics? If so, you'll enjoy the experience of analyzing your codebase with NDepend. Simply download the NDepend trial from https:\\\/\\\/www.ndepend.com\\\/download, and in a few minutes from now, you'll gain a whole new perspective on your .NET code.\",\"sameAs\":[\"https:\\\/\\\/www.ndepend.com\"],\"url\":\"https:\\\/\\\/blog.ndepend.com\\\/author\\\/psmacchia\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# DateTime Format In A Nutshell - NDepend Blog","description":"Use a pattern like \"yyyy-MM-dd HH:mm:ss\" to extract and format the year, month, day, hour, minute, and second from a DateTime into a string","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/","og_locale":"en_US","og_type":"article","og_title":"C# DateTime Format In A Nutshell - NDepend Blog","og_description":"Use a pattern like \"yyyy-MM-dd HH:mm:ss\" to extract and format the year, month, day, hour, minute, and second from a DateTime into a string","og_url":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/","og_site_name":"NDepend Blog","article_published_time":"2025-06-16T01:02:48+00:00","article_modified_time":"2025-07-14T10:22:38+00:00","og_image":[{"width":1254,"height":486,"url":"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png","type":"image\/png"}],"author":"NDepend","twitter_card":"summary_large_image","twitter_misc":{"Written by":"NDepend","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#article","isPartOf":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/"},"author":{"name":"NDepend","@id":"https:\/\/blog.ndepend.com\/#\/schema\/person\/9991dbed4e784e5445b8552129506656"},"headline":"C# DateTime Format In A Nutshell","datePublished":"2025-06-16T01:02:48+00:00","dateModified":"2025-07-14T10:22:38+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/"},"wordCount":1305,"commentCount":0,"image":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png","articleSection":[".NET","C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.ndepend.com\/csharp-datetime-format\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/","url":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/","name":"C# DateTime Format In A Nutshell - NDepend Blog","isPartOf":{"@id":"https:\/\/blog.ndepend.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#primaryimage"},"image":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png","datePublished":"2025-06-16T01:02:48+00:00","dateModified":"2025-07-14T10:22:38+00:00","author":{"@id":"https:\/\/blog.ndepend.com\/#\/schema\/person\/9991dbed4e784e5445b8552129506656"},"description":"Use a pattern like \"yyyy-MM-dd HH:mm:ss\" to extract and format the year, month, day, hour, minute, and second from a DateTime into a string","breadcrumb":{"@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.ndepend.com\/csharp-datetime-format\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#primaryimage","url":"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png","contentUrl":"https:\/\/blog.ndepend.com\/wp-content\/uploads\/C-DateTime-Format-In-A-Nutshell-1.png","width":1254,"height":486,"caption":"C# DateTime Format In A-Nutshell"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.ndepend.com\/csharp-datetime-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.ndepend.com\/"},{"@type":"ListItem","position":2,"name":"C# DateTime Format In A Nutshell"}]},{"@type":"WebSite","@id":"https:\/\/blog.ndepend.com\/#website","url":"https:\/\/blog.ndepend.com\/","name":"NDepend Blog","description":"Improve your .NET code quality with NDepend","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.ndepend.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.ndepend.com\/#\/schema\/person\/9991dbed4e784e5445b8552129506656","name":"NDepend","description":"Do you like reading our blog posts about advanced .NET and C# topics? If so, you'll enjoy the experience of analyzing your codebase with NDepend. Simply download the NDepend trial from https:\/\/www.ndepend.com\/download, and in a few minutes from now, you'll gain a whole new perspective on your .NET code.","sameAs":["https:\/\/www.ndepend.com"],"url":"https:\/\/blog.ndepend.com\/author\/psmacchia\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7bWdu-1xh","_links":{"self":[{"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/posts\/5907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/comments?post=5907"}],"version-history":[{"count":0,"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/posts\/5907\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/media?parent=5907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/categories?post=5907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ndepend.com\/wp-json\/wp\/v2\/tags?post=5907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}