How To Parse String to DateTime in C# (2024)

In this article, we will discuss how to parse string representations of date and time to an equivalent DateTime value in C#. The available methods in the DateTime class include Parse(), TryParse(), ParseExact()and TryParseExact(). These methods can parse a string to a DateTime object differently.

Please refer to DateTime Format In C# to learn more about DateTime format.

To download the source code for this article, you can visit our GitHub repository.

Each method has a list of overloads. Let’s see each of these methods and their overloads more closely. But before then, let’s learn more about customizing culture-related information when working with DateTime objects.

Working With DateTime Objects in C#

When we use the methods in the DateTime class to parse, we need the System.Globalization namespace as it contains classes that define culture-related information, such asthe CultureInfo class, and the DateTimeStyles enum, which we will find helpful when parsing.

Current Culture Settings

The combination of a language and a region is known as a culture. The culture settings of our operating system determine the default date and time format.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!

We can check our current culture using CultureInfo.CurrentCulture and define a different culture using the GetCultureInfo() method if we want to:

DateTime date = new DateTime(2023, 1, 15, 14, 21, 37);Console.WriteLine($"My current culture: {CultureInfo.CurrentCulture}");Console.WriteLine($"My date in the current culture({CultureInfo.CurrentCulture}): {date}");CultureInfo culture = CultureInfo.GetCultureInfo("en-GB");Console.WriteLine($"My date in the culture({culture}): {date.ToString(culture)}");

The current culture should be en-US if we’re based in the US, and the dates are shown in different formats depending on the culture:

My current culture: en-USMy date in the current culture(en-US): 1/15/2023 2:21:37 PMMy date in the culture(en-GB): 15/01/2023 14:21:37

Another way to specify a different culture is to use the CultureInfo class that implements the IFormatProvider interface. Let’s replace the GetCultureInfo() method with a new CultureInfo instance and use the type IFormatProvider:

DateTimedate=newDateTime(2023,1,15,14,21,37);Console.WriteLine($"Mycurrentculture:{CultureInfo.CurrentCulture}");Console.WriteLine($"Mydateinthecurrentculture({CultureInfo.CurrentCulture}):{date}");IFormatProvider provider = new CultureInfo("en-GB");Console.WriteLine($"My date in the culture({provider}): {date.ToString(culture)}");

The result should be the same:

My current culture: en-USMy date in the current culture(en-US): 1/15/2023 2:21:37 PMMy date in the culture(en-GB): 15/01/2023 14:21:37

We’ll use the IFormatProvider interface frequently to define a specific culture in some parsing methods later.

The DateTimeStyles

The DateTimeStyles enum provides formatting options that customize string parsing for some date and time parsing methods. We’ll use a few of the DateTimeStyles enum options for demonstrations later.

Use Parse() to Parse String to DateTime

The DateTime.Parse() method has a list of overloads, and each converts the string data to an equivalent date and time. If the input string cannot be parsed by the CultureInfo associated with the current culture, a FormatException will be thrown.

Let’s take a look at each overload.

Parse(String)

This Parse(String) overload has a string parameter representing the date and time to be parsed. By default, it uses the operating system’s current culture and the DateTimeStyle.None option.

Let’s assume the default culture is en-US:

string dateString = "1/15/2023 02:21:37";DateTime parsedDate = DateTime.Parse(dateString);Console.WriteLine(parsedDate); /* Output: 1/15/2023 2:21:37 AM */

The dateString variable stores a string representation of date and time in the format of MM/dd/yyyy hh:mm:ss. We pass this variable to the Parse() method, and it should parse it to a DateTime object with the format ofM/dd/yyyy hh:mm:ss tt, corresponding to the en-US culture.

If we try to modify the value in the dateString variable to 15/1/2023, it will throw a FormatException:

String '15/01/2023' was not recognized as a valid DateTime.

This is because the parsed DateTime is not valid in the en-US culture as 15 is not considered a valid month.

Parse(String, IFormatProvider)

The previous overload doesn’t allow us to provide a different culture and uses the operating system’s current culture by default. Thankfully, this overload lets us set a culture-specific format using the IFormatProvider parameter.

Let’s try to set the culture to fr-FR and use the string 15/1/2023 02:21:37 this time. Because the new culture will see 15 as an invalid month if we use 1/15/2023 02:21:37:

string dateString = "15/1/2023 02:21:37";IFormatProvider provider = new CultureInfo("fr-FR");DateTime parsedDate = DateTime.Parse(dateString, provider);Console.WriteLine(parsedDate); /* Output: 15/01/2023 2:21:37 AM */

We should expect a parsed DateTime of 15/01/2023 2:21:37 AM.

Parse(String, IFormatProvider, DateTimeStyles)

This overload has an additionalDateTimeStyles parameter that allows us to customize string parsing.

Let’s use the DateTimeStyles.AssumeUniversal for this example:

string dateString = "15/1/2023 02:21:37";IFormatProvider provider = new CultureInfo("fr-FR");DateTimeStyles styles = DateTimeStyles.AssumeUniversal;DateTime parsedDate = DateTime.Parse(dateString, provider, styles);Console.WriteLine(parsedDate); /* Output: 15/01/2023 1:21:37 PM */

Since we don’t specify the time zone, the string format is assumed to be UTC. The parsed DateTime will be 15/01/2023 1:21:37 PM.

Parse(ReadOnlySpan<Char>, IFormatProvider)

This method’s overload is similar to the Parse(String, IFormatProvider) overload, but it has a ReadOnlySpan<Char> parameter instead of a string parameter.

The only difference here is to create a new read-only span over a string using the AsSpan() method:

IFormatProvider provider = new CultureInfo("fr-FR");DateTime parsedDate = DateTime.Parse("15/1/202302:21:37".AsSpan(), provider);Console.WriteLine(parsedDate); /* Output: 15/1/2023 2:21:37 AM */

We should have a similar parsed result of 15/01/2023 2:21:37 AM.

This is a great option to allocate less memory and improve performance. To learn more ways of converting a string to a span, please check out How to Convert a String to a Span in C#.

Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

This overload works the same way as the Parse(String, IFormatProvider, DateTimeStyles) overload, but it uses a ReadOnlySpan<Char> parameter instead.

We’ll set a similar example by using the DateTimeStyles.AssumeUniversal but converting the string to a span this time:

ReadOnlySpan<char> dateSpan = "15/1/2023 02:21:37";IFormatProvider provider = new CultureInfo("fr-FR");DateTimeStyles styles = DateTimeStyles.AssumeUniversal;DateTime parsedDate = DateTime.Parse(dateSpan, provider, styles);Console.WriteLine(parsedDate); /* Output: 15/01/2023 1:21:37 PM */

The parsed DateTime should be similar too, 15/01/2023 1:21:37 PM.

Use TryParse() to Parse String to DateTime

The TryParse() method is a safer and more convenient way of converting strings to DateTime objects.

Instead of throwing an exception if the conversion is unsuccessful, the TryParse() method returns a boolean value indicating whether or not the conversion was successful. Additionally, if the conversion is successful, the out parameter will be populated with the converted value.

Let’s see each overload one by one.

TryParse(String, DateTime)

This is the simplest TryParse() overload with two parameters: a string and a DateTime, which is an out parameter. If we pass a valid string representation of date and time, it will return true along with the converted DateTime object. Otherwise, it will return false with the minimum DateTime value:

string dateString = "1/15/2023";DateTime parsedDate;bool isValidDate = DateTime.TryParse(dateString, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate); /* Output: 1/15/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateString}");

Considering the current culture is en-US, the TryParse() method returns true and the parsedDate variable stores the DateTime1/15/2023 12:00:00 AM.

TryParse(String, IFormatProvider, DateTime)

This overload has an additional IFormatProvider parameter that allows us to set a culture-specific format.

Let’s try to set the culture to en-GB and use the string 15/1/2023 because the new culture will see 15 as an invalid month if we use 1/15/2023:

string dateString = "15/1/2023";DateTime parsedDate;IFormatProvider provider = new CultureInfo("en-GB"); bool isValidDate = DateTime.TryParse(dateString, provider, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate); /* Output: 15/01/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateString}");

We should expect the isValidDate variable to contain a true value along with a parsed DateTime of 15/01/2023 12:00:00 AM in the parsedDate variable. In contrast, if we pass the string 1/15/2023, it will return false with a minimum DateTime value.

TryParse(String, IFormatProvider, DateTimeStyles, DateTime)

This TryParse() overload has an additionalDateTimeStyles parameter that allows us to customize string parsing.

Let’s use the DateTimeStyles.AdjustToUniversal for this example:

string dateString = "2023/1/15 14:21:37-05:00";DateTime parsedDate;IFormatProvider provider = new CultureInfo("en-GB"); DateTimeStyles styles = DateTimeStyles.AdjustToUniversal;bool isValidDate = DateTime.TryParse(dateString, provider, styles, out parsedDate); if (isValidDate) Console.WriteLine(parsedDate); /* Output: 15/01/2023 7:21:37 PM */else Console.WriteLine($"Unable to parse date:{dateString}");

Using AdjustToUniversal as the DateTimeStyle option means that the parsedDate variable will receive the date and time in Coordinated Universal Time (UTC) format. If we pass a date and time with the EST time zone, we should get a parsed DateTime 15/01/2012 7:21:37 PM.

TryParse(ReadOnlySpan<Char>, DateTime)

This overload is similar to the TryParse(String, DateTime) overload, but it has a ReadOnlySpan<Char> parameter instead of a string parameter.

The only difference here is to create a new read-only span over a string:

ReadOnlySpan<char> dateSpan = "1/15/2023"; DateTime parsedDate;bool isValidDate = DateTime.TryParse(dateSpan, out parsedDate); if (isValidDate) Console.WriteLine(parsedDate); /* Output: 1/15/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateSpan}");

Since the current culture is en-US and if the conversion is successful, the parse method returns true and the parsedDate variable stores the DateTime1/15/2023 12:00:00 AM.

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime)

This method’s overload works the same way as the TryParse(String, IFormatProvider, DateTime) overload, but it uses a ReadOnlySpan<Char> parameter instead.

We’ll use the same example but convert the string to a span this time:

ReadOnlySpan<char> dateSpan = "15/1/2023"; DateTime parsedDate;IFormatProvider provider = new CultureInfo("en-GB");bool isValidDate = DateTime.TryParse(dateSpan, provider, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate); /* Output: 15/01/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateSpan}");

The parsed DateTime should be similar, 15/01/2023 12:00:00 AM.

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

Finally, this overload isn’t too different from the TryParse(String, IFormatProvider, DateTimeStyles) overload, the only difference is it uses a ReadOnlySpan<Char> parameter.

We’ll also use the same example but with a span. To keep it simple, we’ll set the DateTimeStyles.None option this time:

ReadOnlySpan<char> dateSpan = "15/1/2023"; DateTime parsedDate;IFormatProvider provider = new CultureInfo("en-GB"); DateTimeStyles styles = DateTimeStyles.None;bool isValidDate = DateTime.TryParse(dateSpan, provider, styles, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate, provider); /* Output: 15/01/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateSpan}");

Since we don’t set a specific DateTimeStyle, the parsed DateTime should be 15/01/2023 12:00:00 AM.

Use ParseExact() to Parse String to DateTime

TheDateTime.ParseExact() method has some overloaded forms, each allowing for different arguments and options to be passed. This method is used to parse a string that is formatted exactly as specified by the format string argument and culture-specific format information, and it throws a FormatException if the string cannot be parsed into a DateTime value that matches the exact format string.

Let’s go to the overloads.

ParseExact(String, String, IFormatProvider)

This overload has three input parameters: two strings and an IFormatProvider. The first parameter is a string representation of date and time, the second one defines the exact format, and we can set a culture-specific format using the last parameter.

Let’s see how it works:

string dateString = "15/1/2023 10:12:12";string format = "dd/M/yyyy hh:mm:ss";DateTime parsedDate;IFormatProvider provider = new CultureInfo("fr-FR");try{ parsedDate = DateTime.ParseExact(dateString, format, provider); Console.WriteLine(parsedDate); /* Output: 15/01/2023 10:12:12 AM */}catch(FormatException ex){ Console.WriteLine(ex.Message);}

The dateString variable receives the string representation of date and time15/1/2023 10:12:12, with the format of dd/M/yyyy hh:mm:ss. The Parse() method will parse the string to a DateTime in fr-FR format 15/01/2023 10:12:12 AM.

Note that the format of the string representation must match the specified format exactly. Else itwill throw a FormatException if we try to edit the format slightly differently, for example dd/MM/yyyy hh:mm:ss:

String '15/1/2023 10:12:12' was not recognized as a valid DateTime.

ParseExact(String, String, IFormatProvider, DateTimeStyles)

Unlike the prior overload, we have an additional DateTimeStyles parameter for this ParseExact() overload. Let’s try to round-trip a DateTime value this time:

string dateString = "2023-01-15T14:12:12.0000000Z";DateTime parsedDate;IFormatProvider provider = new CultureInfo("fr-FR");styles = DateTimeStyles.RoundtripKind;try{ parsedDate = DateTime.ParseExact(dateString, "o", provider, styles); Console.WriteLine(parsedDate); /* Output: 15/01/2023 2:12:12 PM */}catch (FormatException ex){ Console.WriteLine(ex.Message);}

The dateString variable receives the string representation of date and time 2023-01-15T14:12:12.0000000Z. This represents an ISO 8601 string which includes time-zone information.

We use the o round-tripping format specifier to specify the format and the RoundtripKindDateTimeStyle.

We should expect a converted DateTime in fr-FR format 15/01/2023 2:12:12 PM.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

This overload is quite similar to the previous overload, but this uses ReadOnlySpan<char> instead of a string. We’ll create a new read-only span over a string using implicit conversion from a string:

ReadOnlySpan<char> dateSpan = "2023-01-15T14:12:12.0000000Z";DateTime parsedDate;IFormatProvider provider = new CultureInfo("fr-FR");DateTimeStyles styles = DateTimeStyles.RoundtripKind;try{ parsedDate = DateTime.ParseExact(dateSpan, "o", provider, styles); Console.WriteLine(parsedDate); /* Output: 15/01/2023 2:12:12 PM */}catch (FormatException ex){ Console.WriteLine(ex.Message);}

It should produce the same result by passing similar arguments as the previous example to this overload. Using span is a good option to allocate less memory and improve performance.

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

This method’s overload is similar to the ParseExact(String, String, IFormatProvider, DateTimeStyles) overload, but the second parameter can accept an array of exact date formats this time:

string[] formats = { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyyMMdd'T'HH:mm:ss.fff'Z'", "yyyyMMdd'T'HH:mm:ss'Z'", "dd-MM-yyyy HH:mm:ss", "dd/MM/yyyy HH:mm:ss" };IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;var dateStringsByFormat = new Dictionary<string, string>{ { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "2023-01-15T14:12:12.000Z" }, { "yyyy-MM-dd'T'HH:mm:ss'Z'", "2023-01-15T14:12:12Z" }, { "yyyyMMdd'T'HH:mm:ss.fff'Z'", "20230115T14:12:12.000Z" }, { "yyyyMMdd'T'HH:mm:ss'Z'", "20230115T14:12:12Z" }, { "dd-MM-yyyy HH:mm:ss", "15-01-2023 14:12:12" }, { "dd/MM/yyyy HH:mm:ss", "15/01/2023 14:12:12" }};try{ foreach (string format in formats) { string dateString = dateStringsByFormat[format]; DateTime parsedDate = DateTime.ParseExact(dateString, formats, provider, styles); Console.WriteLine(parsedDate); }}catch (FormatException ex){ Console.WriteLine(ex.Message);}

We provide multiple formats in a string array and then create a collection of key-value pairs representing the format and the string representation of date and time. The list of keys is similar to the ones in the formats array.

To demonstrate a simpler example, we use an InvariantCulture culture and do not specify a DateTimeStyle.

We then loop through the array and get the string representation of date and time from the list of key-value pairs using format as the key. We pass the string as the first argument and the formats array as the second argument to the overload. The parsing should be successful if the array of allowable formats recognizes the string format.

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

For the last overload for the DateTime.ParseExact() method, it’s similar to the previous overload, but the first parameter takes a ReadOnlySpan<char> instead of a string:

string[] formats = { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyyMMdd'T'HH:mm:ss.fff'Z'", "yyyyMMdd'T'HH:mm:ss'Z'", "dd-MM-yyyy HH:mm:ss", "dd/MM/yyyy HH:mm:ss" };IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;var dateStringsByFormat = new Dictionary<string, string>{ { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "2023-01-15T14:12:12.000Z" }, { "yyyy-MM-dd'T'HH:mm:ss'Z'", "2023-01-15T14:12:12Z" }, { "yyyyMMdd'T'HH:mm:ss.fff'Z'", "20230115T14:12:12.000Z" }, { "yyyyMMdd'T'HH:mm:ss'Z'", "20230115T14:12:12Z" }, { "dd-MM-yyyy HH:mm:ss", "15-01-2023 14:12:12" }, { "dd/MM/yyyy HH:mm:ss", "15/01/2023 14:12:12" }};try{ foreach (string format in formats) { ReadOnlySpan<char> dateSpan = dateStringsByFormat[format]; DateTime parsedDate = DateTime.ParseExact(dateSpan, formats, provider, styles); Console.WriteLine(parsedDate); }}catch (FormatException ex){ Console.WriteLine(ex.Message);}

We use similar values as the previous example, but this time we’ll first create a new read-only span over a stringbefore parsing.

Use TryParseExact() to Parse String to DateTime

The DateTime.TryParseExact() method works like the DateTime.TryParse() method, but the format of the string representation must match the specified format exactly like the DateTime.ParseExact() method.

We’ve seen a considerable amount of examples using different DateTimeStyles and IFormatProvider in the previous overloads, so we’ll keep them simple in the upcoming examples by using the CultureInfo.InvariantCulture and DateTimeStyle.None values.

Let’s look at each overload.

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime)

The first four parameters in this overload are similar to the ones in the ParseExact(String, String, IFormatProvider, DateTimeStyles) overload, but with an additional out DateTime parameter.

Let’s try to parse using this overload:

string dateString = "15-01-2023";string format = "dd-MM-yyyy";DateTimeparsedDate;IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;bool isValidDate = DateTime.TryParseExact(dateString, format, provider, styles, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate); /* Output: 1/15/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateString}");

The dateString variable stores the string representation of the date 15-1-2023, with the format of dd-MM-yyyy. By using the CultureInfo.InvariantCulture and DateTimeStyle.None, we should expect the parsed DateTime to be 1/15/2023 12:00:00 AM.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime)

This TryParseExact() overload is similar to the previous overload, except that we use ReadOnlySpan<Char> in the first two parameters:

ReadOnlySpan<char> dateSpan = "15-01-2023";ReadOnlySpan<char> formatSpan = "dd-MM-yyyy";DateTime parsedDate;IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;bool isValidDate = DateTime.TryParseExact(dateSpan, formatSpan, provider, styles, out parsedDate);if (isValidDate) Console.WriteLine(parsedDate); /* 1/15/2023 12:00:00 AM */else Console.WriteLine($"Unable to parse date:{dateSpan}");

Using the same example, we should expect a similar result of a valid parsed DateTime of 1/15/2023 12:00:00 AM.

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)

We’ve previously introduced the string array parameter that accepts multiple formats in the ParseExact(String, String[], IFormatProvider, DateTimeStyles) overload. This overload is similar but returns an out parameter.

We’ll be using the same example:

string[] formats = { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", ..., "dd/MM/yyyy HH:mm:ss" };IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;var dateStringsByFormat = new Dictionary<string, string>{ { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "2023-01-15T14:12:12.000Z" }, ...};foreach (var format in formats){ string dateString = dateStringsByFormat[format]; DateTime parsedDate; bool isValidDate = DateTime.TryParseExact(dateString, formats, provider, styles, out parsedDate); if (isValidDate) Console.WriteLine(parsedDate); else Console.WriteLine($"Unable to parse date:{dateString}");}

We loop through the formats array, get the string value from the list of key-value pairs, and try to parse the string to a DateTime that is recognizable within the formats array. Note that we pass the string as the first argument and the formats array as the second argument to the overload. If the parsing is successful, the method should return true and a converted DateTime each time it’s triggered.

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime)

This last overload is similar to the previous overload but uses span for the first parameter. We’ll be using the same example, but we’ll convert the string representation of the date and time to span each time we want to call the overload:

string[] formats = { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", ... "dd/MM/yyyy HH:mm:ss" };IFormatProvider provider = CultureInfo.InvariantCulture;DateTimeStyles styles = DateTimeStyles.None;var dateStringsByFormat = new Dictionary<string, string>{ { "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", "2023-01-15T14:12:12.000Z" }, ...};foreach (var format in formats){ ReadOnlySpan<char> dateSpan = dateStringsByFormat[format]; DateTime parsedDate; bool isValidDate = DateTime.TryParseExact(dateSpan, formats, provider, styles, out parsedDate); if (isValidDate) Console.WriteLine(parsedDate); else Console.WriteLine($"Unable to parse date:{dateSpan}");}

Since each string representation of the date and time passed has a format that is identifiable within the formats array, each TryParseExact() method call should return true and a successfully parsed DateTime.

Conclusion

In this article, we’ve learned that in C#, we have four different methods Parse(), TryParse(), ParseExact()and TryParseExact() with their overloads in the DateTime class to parse a string representation of date and time to a DateTime object, and we can use them according to our needs.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!

How To Parse String to DateTime in C# (2024)

FAQs

How to parse a string to DateTime in C#? ›

string date = "16/09/2019"; DateTime result = DateTime. Parse(date); Console. WriteLine(result); In the above example, the output will be 09/16/2019 12:00:00 AM , which is the DateTime equivalent of the string “16/09/2019”.

How to parse a string to DateTime? ›

strptime() In Python, we can use the datetime. strptime() method to convert a string to a datetime object. The strptime() method takes two arguments: the string to be converted and a format string specifying the input string's format.

How to convert list of strings to DateTime in C#? ›

There are many ways to convert a string to DateTime. C# provides many methods like Parse(), ParseExact(), TryParse() and TryParseExact(). In python datetime. strptime() method is used.

How to get DateTime string in C#? ›

ToString("MM-dd-yyyy HH:mm:ss")); In this example, we're using the ToString method with a pattern like "MM-dd-yyyy HH:mm:ss". This pattern instructs C# to display the DateTime as month, day, year, hour, minute, and second, separated by dashes and colons.

How to parse a string in C#? ›

using System; public static class StringConversion { public static void Main() { string input = String.Empty; try { int result = Int32.Parse(input); Console.WriteLine(result); } catch (FormatException) { Console.WriteLine($"Unable to parse '{input}'"); } // Output: Unable to parse '' try { int numVal = Int32.Parse("- ...

How to convert string to DateTime with custom format in C#? ›

Parse() and ParseExact() are quite similar. However, in ParseExact(), we can pass format as an extra parameter not available in Parse(). Format parameter helps to convert a string date value to a DateTime object when a date is a different format like "11-23-2015" (Format should be "MM-dd-yyyy").

How to parse date only in C#? ›

ParseExact(String, String[], IFormatProvider, DateTimeStyles) Converts the specified string representation of a date to its DateOnly equivalent using the specified array of formats, culture-specific format information, and style.

How to extract date from string in C#? ›

Follow the below steps to implement the idea:
  1. Create a regex expression to extract all the present dates from the string.
  2. Use Pattern class to compile the regex formed.
  3. Use the matcher function to find.
  4. If it is valid, return true. Otherwise, return false.
Mar 23, 2023

How to convert string to date in .NET Core? ›

Convert a String to a Date

Parse(dateString); for any important work because the string you have might be in an unexpected format that parses into a DateTime without error, but is not the date you expected. If you have a complex custom date format, you can specify it exactly using . NET custom string components.

What is the difference between Parse and ParseExact? ›

ParseExact(“11/10/2023 15:30:00”, “dd/MM/yyyy HH:mm:ss”, CultureInfo. InvariantCulture); The key difference between the two is the level of flexibility. “Parse” is more lenient and can infer the format to some extent, while “ParseExact” requires you to specify the exact format of the input string.

How to use DateTime.ParseExact in C#? ›

dateString = "06/15/2008"; format = "d"; try { result = DateTime. ParseExact(dateString, format, provider); Console. WriteLine("{0} converts to {1}.", dateString, result. ToString()); } catch (FormatException) { Console.

How to convert DateTime to string in specific format in C#? ›

To format it using the general date and time format specifier ('G') for a specific culture, call the ToString(IFormatProvider) method. To format it using a specific date and time format specifier and the conventions of a specific culture, call the ToString(String, IFormatProvider) method.

How to Parse string to DateTime in C#? ›

WriteLine(parsedDate); /* Output: 1/15/2023 12:00:00 AM */ else Console. WriteLine($"Unable to parse date:{dateString}"); string dateString = "15-01-2023"; string format = "dd-MM-yyyy"; DateTime parsedDate; IFormatProvider provider = CultureInfo. InvariantCulture; DateTimeStyles styles = DateTimeStyles.

How do you convert a string to a DateTime? ›

Strptime() The Strptime() is available in the datetime module and is used for Date-Time conversion. This function changes the given string of Datetime into the desired format. Example: The program defines a function that converts the date and time represented as a string to a datetime object using the desired format.

How to convert string to valid DateTime in C#? ›

However, if you want to use ParseExact, you need to do this: testTime = DateTime. ParseExact(test, "yyyy-MM-ddTHH:ssZ", System.

How to extract date from a string in C#? ›

Follow the below steps to implement the idea:
  1. Create a regex expression to extract all the present dates from the string.
  2. Use Pattern class to compile the regex formed.
  3. Use the matcher function to find.
  4. If it is valid, return true. Otherwise, return false.
Mar 23, 2023

How to convert empty string to DateTime in C#? ›

ConvertToDateTime(dateString) ' Convert an empty string. dateString = String. Empty ConvertToDateTime(dateString) ' Convert a non-date string.

How to parse timestamp from string? ›

2. Parse a String to a Timestamp
  1. 2.1. Standard Format. The simplest way to parse a String to a Timestamp is its valueOf method: Timestamp.valueOf("2018-11-12 01:02:03.123456789") ...
  2. 2.2. Alternative Formats. Now, if it isn't in JDBC timestamp format, then luckily, valueOf also takes a LocalDateTime instance.
Jan 8, 2024

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5371

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.