{"id":1830,"date":"2023-05-15T10:04:52","date_gmt":"2023-05-15T03:04:52","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1830"},"modified":"2023-05-22T06:56:56","modified_gmt":"2023-05-21T23:56:56","slug":"csharp-datetime-parseexact","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-datetime-parseexact\/","title":{"rendered":"C# DateTime ParseExact"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the C# <code>DateTime<\/code> <code>ParseExact()<\/code> method to convert a <code>string<\/code> to a <code>DateTime<\/code> object with the exact matched format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# DateTime ParseExact() method<\/h2>\n\n\n\n<p>If you have a date and time string with a specified format, you can convert it to a <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-datetime\/\">DateTime<\/a><\/code> object using the <code>ParseExact()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> DateTime <span class=\"hljs-title\">ParseExact<\/span> (<span class=\"hljs-params\">\n   <span class=\"hljs-keyword\">string<\/span> s, \n   IFormatProvider? provider, \n   System.Globalization.DateTimeStyles styles\n<\/span>)<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>ParseExact()<\/code> method takes three parameters:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>string s<\/code>: The string representation of the date and time to convert.<\/li>\n\n\n\n<li><code>IFormatProvider? provider<\/code>: An optional <code>IFormatProvider<\/code> object that specifies the culture-specific format information used to interpret the date and time string. If <code>null<\/code>, the method uses the default format provider for the current culture.<\/li>\n\n\n\n<li><code>System.Globalization.DateTimeStyles styles<\/code>: A <code>DateTimeStyles<\/code> value that specifies any additional style information to use when interpreting the date and time string. For example, you can specify whether to allow leading\/trailing white space, whether to allow a date to have no year, or whether to allow a time to have no seconds.<\/li>\n<\/ol>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Globalization;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> s = <span class=\"hljs-string\">\"05\/07\/2023\"<\/span>;\n\n<span class=\"hljs-keyword\">var<\/span> dt = DateTime.ParseExact(\n    s,\n    <span class=\"hljs-string\">\"M\/d\/yyyy\"<\/span>,\n    CultureInfo.InvariantCulture);\n\nWriteLine(dt.ToLongDateString()); <span class=\"hljs-comment\">\/\/ Sunday, May 7, 2023<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we use the <code>ParseExact()<\/code> method to convert the date string <code>\"05\/07\/2023\"<\/code> to a <code>DateTime<\/code> with the format <code>M\/d\/yyyy<\/code>. <\/p>\n\n\n\n<p>The <code>ParseExact()<\/code> accepts three arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>s<\/code> is the input string that represents a date and time.<\/li>\n\n\n\n<li><code>\"M\/d\/yyyy\" <\/code>is the specified format for parsing the input string s.<\/li>\n\n\n\n<li><code>CultureInfo.InvariantCulture<\/code> is the culture for parsing the input string. The <code>CultureInfo.InvariantCulture<\/code> means culture-independent.<\/li>\n<\/ul>\n\n\n\n<p>For a complete list of custom date and time format specifiers, please check out <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/custom-date-and-time-format-strings\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">this page<\/a>.<\/p>\n\n\n\n<p>Sometimes, the date string may have one or many possible formats. To match a date string with multiple possible formats, you can pass an array of formats to the second argument of the ParseExact method like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> DateTime <span class=\"hljs-title\">ParseExact<\/span> (<span class=\"hljs-params\">\n   <span class=\"hljs-keyword\">string<\/span> s, \n   <span class=\"hljs-keyword\">string<\/span>&#91;] formats, \n   IFormatProvider? provider, \n   System.Globalization.DateTimeStyles style\n<\/span>)<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Globalization;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> s = <span class=\"hljs-string\">\"15\/07\/2023\"<\/span>;\n<span class=\"hljs-keyword\">string<\/span>&#91;] formats = { <span class=\"hljs-string\">\"M\/d\/yyyy\"<\/span>, <span class=\"hljs-string\">\"d\/M\/yyyy\"<\/span> };\n<span class=\"hljs-keyword\">var<\/span> culture = CultureInfo.InvariantCulture;\n<span class=\"hljs-keyword\">var<\/span> style = DateTimeStyles.None;\n\n<span class=\"hljs-keyword\">try<\/span>\n{\n    <span class=\"hljs-keyword\">var<\/span> dt = DateTime.ParseExact(s, formats, culture, style);\n    WriteLine(<span class=\"hljs-string\">\"Converted '{0}' to {1}.\"<\/span>, s, dt);\n}\n<span class=\"hljs-keyword\">catch<\/span> (FormatException)\n{\n    WriteLine(<span class=\"hljs-string\">\"Unable to convert '{0}'.\"<\/span>, s);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we have a string <code>s<\/code> that represents a date in the format <code>15\/07\/2023<\/code>. We want to convert this string to its corresponding <code>DateTime<\/code> object, but we don&#8217;t know which format the date is in.<\/p>\n\n\n\n<p>To handle this situation, we call the <code>DateTime.ParseExact()<\/code> method with multiple formats specified in the <code>formats<\/code> array. In this case, we have two expected formats: <code>\"M\/d\/yyyy\"<\/code> and <code>\"d\/M\/yyyy\"<\/code>. This means that the method will try to parse the input string using both formats until it finds a match.<\/p>\n\n\n\n<p>If the input string <code>s<\/code> matches one of the expected formats, the method returns a <code>DateTime<\/code> value that corresponds to the parsed date and time string. If the input string doesn&#8217;t match any of the expected formats, the method throws a <code>FormatException<\/code> exception.<\/p>\n\n\n\n<p>In this example, the output will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Converted '15\/07\/2023' to 7\/15\/2023 12:00:00 AM.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>indicating that the string has been successfully converted to a <code>DateTime<\/code> object using the <code>\"M\/d\/yyyy\"<\/code> format.<\/p>\n\n\n\n<p>If the <code>ParseExact()<\/code> cannot parse the input string based on the specified format, it&#8217;ll throw a <code>FormatException<\/code> exception. To avoid the exception, you can use the <code>TryParseExact()<\/code> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# DateTime TryParseExact() method<\/h2>\n\n\n\n<p>The <code>TryParseExact()<\/code> method is the same as the <code>ParseExact()<\/code> method except that it doesn&#8217;t throw a <code>FormatException<\/code> exception if the input string is not matched with the date format specifier. Instead, it returns <code>true<\/code> if the conversion succeeds or <code>false<\/code> otherwise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-title\">TryParseExact<\/span> (<span class=\"hljs-params\">\n    <span class=\"hljs-keyword\">string<\/span>? s, \n    <span class=\"hljs-keyword\">string<\/span>? format, \n    IFormatProvider? provider, \n    System.Globalization.DateTimeStyles style, \n    <span class=\"hljs-keyword\">out<\/span> DateTime result\n<\/span>)<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>TryParseExact()<\/code> takes several parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>string s<\/code>: The string representation of the date and time to convert.<\/li>\n\n\n\n<li><code>string format<\/code>: A string that specifies the expected format of the date and time string.<\/li>\n\n\n\n<li><code>CultureInfo culture<\/code>: A <code>CultureInfo<\/code> object that specifies the culture used to interpret the date and time string. This affects the interpretation of any format specifiers that use culture-sensitive information, such as the day of the week or the month name.<\/li>\n\n\n\n<li><code>DateTimeStyles styles<\/code>: A <code>DateTimeStyles<\/code> value that specifies any additional style information to use when interpreting the date and time string. For example, you can specify whether to allow leading\/trailing white space, whether to allow a date to have no year, or whether to allow a time to have no seconds.<\/li>\n\n\n\n<li><code>out DateTime result<\/code>: An <code>out<\/code> parameter that receives the <code>DateTime<\/code> value that corresponds to the parsed date and time string if the conversion is successful.<\/li>\n<\/ul>\n\n\n\n<p>The following example shows how to use the <code>TryParseExact()<\/code> method to parse a date string using a list of formats:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> System.Globalization;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">var<\/span> s = <span class=\"hljs-string\">\"15\/07\/2023\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> formats = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">string<\/span>&#91;] { <span class=\"hljs-string\">\"M\/d\/yyyy\"<\/span>, <span class=\"hljs-string\">\"d\/M\/yyyy\"<\/span> };\n<span class=\"hljs-keyword\">var<\/span> culture = CultureInfo.InvariantCulture;\n<span class=\"hljs-keyword\">var<\/span> style = DateTimeStyles.None;\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> format <span class=\"hljs-keyword\">in<\/span> formats)\n{\n    <span class=\"hljs-keyword\">if<\/span> (DateTime.TryParseExact(s, format, culture, style, <span class=\"hljs-keyword\">out<\/span> <span class=\"hljs-keyword\">var<\/span> dt))\n    {\n        WriteLine(dt);\n    }\n    <span class=\"hljs-keyword\">else<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">$\"The string <span class=\"hljs-subst\">{s}<\/span> doesn't match the format <span class=\"hljs-subst\">{format}<\/span>\"<\/span>);\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">The string 15\/07\/2023 doesn't match the format M\/d\/yyyy\n7\/15\/2023 12:00:00 AM<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, initialize the date string s with the value 15\/07\/2023, a string array <code>formats<\/code> with two different date format strings: <code>\"M\/d\/yyyy\"<\/code> and <code>\"d\/M\/yyyy\"<\/code>. We&#8217;ll use these formats to parse the date string <code>s<\/code>.<\/p>\n\n\n\n<p>Second, declare a <code>CultureInfo<\/code> object called culture with the value <code>InvariantCulture<\/code>. It ensures that the <code>TryParseExact<\/code> will parse the date string correctly regardless of the user&#8217;s cultural settings.<\/p>\n\n\n\n<p>Third, define a <code>DateTimeStyles<\/code> variable called <code>style<\/code> with the value <code>None<\/code>. This specifies that the <code>TryParseExact()<\/code> won&#8217;t need any additional styles or flags when parsing the date string.<\/p>\n\n\n\n<p>Finally, use a <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-foreach\/\">foreach<\/a><\/code> loop that iterates through each of the date formats in the formats array. For each format, use the <code>DateTime.TryParseExact()<\/code> method to parse the <code>s<\/code> string using the specified format, culture, and style.<\/p>\n\n\n\n<p>If the conversion is successful, the program writes the resulting <code>DateTime<\/code> object to the console using the <code>WriteLine()<\/code> method. Otherwise, it writes an error message to the console indicating that the string <code>s<\/code> does not match the current format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the C# DateTime <code>ParseExact()<\/code> method to convert a string to a DateTime object with the exact matched format.<\/li>\n\n\n\n<li>Use the C# <code>DateTime.TryParseExact()<\/code> method to convert a string representation of a date and time to its <code>DateTime<\/code> equivalent using a specified format and culture, and returns true if the conversion was successful or false otherwise.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1830\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-datetime-parseexact\/\"\n\t\t\t\tdata-post-title=\"C# DateTime ParseExact\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1830\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-datetime-parseexact\/\"\n\t\t\t\tdata-post-title=\"C# DateTime ParseExact\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the C# DateTime ParseExact() method to convert a string to a DateTime object with the exact matched format. Introduction to the C# DateTime ParseExact() method If you have a date and time string with a specified format, you can convert it to a DateTime object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":81,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1830","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1830"}],"version-history":[{"count":3,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1830\/revisions"}],"predecessor-version":[{"id":1866,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1830\/revisions\/1866"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}