C# - Parsing a DateTime from a string | makolyte (2024)

You can convert a string to a DateTime (parse) with one of these methods:

  • DateTime.Parse() (or TryParse). Use this to parse a variety of standard formats (culture-specific or ISO-8601)
  • DateTime.ParseExact() (or TryParseExact). Use this to parse from a specific format.

I’ll show examples of using both methods for parsing various DateTime formats.

Using DateTime.Parse()

DateTime.Parse() can handle parsing a wide variety of culture-specific standard DateTime formats and the ISO-8601 format (which is the default date format for JSON serialization). By default, it uses the current culture. If there’s partial information (such as just the date), it makes assumptions and fills in information.

Here’s an example of using DateTime.Parse() to parse several standard DateTime formats:

// Culture-specific standard formats// Note: This is using my current culture (en-US) with date format: M/d/yyyy h:mm:ss AM|PMvar dateAndTime = DateTime.Parse("5/3/2023 8:15:10 AM");Console.WriteLine(dateAndTime); //Outputs: 5/3/2023 8:15:10 AMvar longDate = DateTime.Parse("Wednesday, May 3, 2023");Console.WriteLine(longDate); //Outputs: 5/3/2023 12:00:00 AM (it assumes 12:00)var dateOnly = DateTime.Parse("5/3/2023");Console.WriteLine(dateOnly); //Outputs: 5/3/2023 12:00:00 AM (it assumes 12:00)var timeOnly = DateTime.Parse("08:15:10");Console.WriteLine(timeOnly); //Outputs: 5/3/2023 8:15:10 AM (it assumes today's date)// ISO-8601 standard formatvar iso8601_withTimeZone = DateTime.Parse("2023-05-03T08:15:10.4068309-04:00");Console.WriteLine(iso8601_withTimeZone); //Outputs: 5/3/2023 8:15:10 AMvar iso8601_UTC = DateTime.Parse("2023-05-03T12:15:10.4068309Z");Console.WriteLine(iso8601_UTC); //Outputs: 5/3/2023 8:15:10 AMCode language: C# (cs)

Read more about how to test that you’re able to handle other culture’s date formats.

If DateTime.Parse() can’t figure out the format automatically, it throws a FormatException. You can use DateTime.TryParse() for better error handling. Here’s an example:

if (DateTime.TryParse("050323", out DateTime result)){ //use result}else{ //It wasn't able to parse automatically}Code language: C# (cs)

Using DateTime.ParseExact()

You can use DateTime.ParseExact() to parse a specific DateTime format. Use this in two cases:

  • You want to parse a custom (non-standard) DateTime format.
  • You *only* want to parse a specific DateTime format (standard or not).

Here are examples of using DateTime.ParseExact() with different format strings:

//Note: Console.WriteLine is outputting using my current culture (en-US) format (M/d/yyyy h:mm:ss AM|PM)//Custom formatsvar dateMMddyy = DateTime.ParseExact("050323", format: "MMddyy", CultureInfo.InvariantCulture);Console.WriteLine(dateMMddyy); //Outputs: 5/3/2023 12:00:00 AM (assumes 12:00 AM)var dateyyyyMMdd = DateTime.ParseExact("20230503", format: "yyyyMMdd", CultureInfo.InvariantCulture);Console.WriteLine(dateyyyyMMdd); //Outputs: 5/3/2023 12:00:00 AM (assumes 12:00 AM)var MMddyy = DateTime.ParseExact("2023-03-05", format: "yyyy-dd-MM", CultureInfo.InvariantCulture);Console.WriteLine(MMddyy); //Outputs: 5/3/2023 12:00:00 AM (assumes 12:00 AM)//ISO-8601 formatsvar iso8601_WithTimeZone = "2023-05-03T08:15:10.4068309-04:00";var dateISO8601 = DateTime.ParseExact(iso8601_WithTimeZone, format: "O", CultureInfo.InvariantCulture);Console.WriteLine(dateISO8601); //Outputs: 5/3/2023 8:15:10 AMvar iso8601_UTC = "2023-05-03T12:15:10.4068309Z";var dateUTC = DateTime.ParseExact(iso8601_UTC, format: "O", CultureInfo.InvariantCulture);Console.WriteLine(dateUTC); //Outputs: 5/3/2023 8:15:10 AMCode language: C# (cs)

Note: This overload of ParseExact() uses DateTimeStyles.None internally, so you don’t need to pass that in.

If DateTime.ParseExact() can’t parse to the exact DateTime format you specified, it throws a FormatException. Use DateTime.TryParseExact() for better error handling. Here’s an example:

if (DateTime.TryParseExact(dateString, format: "MMddyy",  CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result)){ //use result}else{ //It wasn't able to parse automatically}Code language: C# (cs)

DateTime format specifiers

When you’re using DateTime.ParseExact(), you pass it a format string. These are the same format strings you use when converting a DateTime to a string. This consists of a combination of format specifier characters (such as “MM”, “yyyy”) or a single character (such as “o”). Here’s a quick reference table for common formats:

Format specifierDescriptionExample
dd/MM/yyyyday of month (2 digits), month (2 digits), year (4 digits)03/05/2023 (May 3rd, 2023)
M/d/yyyymonth (1-2 digits), day (1-2 digits), year (4 digits)5/3/2023 (May 3rd, 2023)
12/25/2023 (December 25th, 2023)
yyyy-MM-ddyear (4 digits), month (2 digits), day (2 digits)2023-05-03 (May 3rd, 2023)
yyyyMMddyear (4 digits), month (2 digits), day (2 digits)20230503 (May 3rd, 2023)
HH:mm24-hour time14:15 (2:15 pm)
oISO-8601 format2023-05-03T12:15:10.4068309Z

In Visual Studio, when you start typing in the format string, IntelliSense pops up a full list of individual format specifier characters you can use. In addition, if you want an exhaustive list, check out this MSDN page about all of the possible format specifiers.

Parsing a two-digit year

We all know it’s a bad idea to use two-digit years. When you use a two-digit year, you lose information and have to guess what four-digit year it represents.

When you parse a two-digit year with DateTime.ParseExact(), it uses CultureInfo.Calendar.TwoDigitYearMax to determine how to parse it into a four-digit year. The default TwoDigitYearMax on my system right now is 2049. You can pass in a CultureInfo and set TwoDigitYearMax to whatever value makes sense in your scenario.

Here’s an example. Let’s say I need to parse “9912” into “yyMM” and I know I’m dealing with future dates in the current century. If I let it use the default TwoDigitYearMax, it will parse this into “12/1/1999”. I really want it to parse into “12/1/2099”, so I need to set TwoDigitYearMax=2100, like this:

string date = "9912";CultureInfo culture = new CultureInfo("en-US");culture.Calendar.TwoDigitYearMax = 2100;if (DateTime.TryParseExact(date, "yyMM", culture, DateTimeStyles.None, out DateTime dt)){Console.WriteLine($"Parsed {date} into {dt}");}else{Console.WriteLine($"Unable to parse {date}");}Code language: C# (cs)

This output the following:

Parsed 9912 into 12/1/2099 12:00:00 AMCode language: plaintext (plaintext)

The current year is 2023. What if I want to assume the two-digit year always starts at the current year? I can set TwoDigitYearMax to the current year (from DateTime.Now.Year) + 99, like this:

string date = "2112";CultureInfo culture = new CultureInfo("en-US");culture.Calendar.TwoDigitYearMax = DateTime.Now.Year + 99;if (DateTime.TryParseExact(date, "yyMM", culture, DateTimeStyles.None, out DateTime dt)){Console.WriteLine($"Parsed {date} into {dt}");}else{Console.WriteLine($"Unable to parse {date}");}Code language: C# (cs)

This output the following:

Parsed 2112 into 11/1/2023 12:00:00 AMCode language: plaintext (plaintext)

Dealing with uncertainty with a two-digit year

In the examples above I mentioned that I knew I was dealing with future dates. What if you’re dealing with dates that could be in either the future or past, or have more than a 100 year range?

I can think of two examples:

  • Birthdates
  • Expiration dates

If you were parsing data with birthdates that had two-digit years, you won’t be able to say with 100% certainty that you’re going to guess the correct four-digit year.

For example, let’s say you get the birthdate: “1/1/19”. Logically, all birthdates will be in the past, so you could set the TwoDigitYearMax = the current year of 2021. But this birthdate could realistically be either “1/1/1999” or “1/1/1919”, since there are definitely people who are over 100 years old.

How would you deal with this? My recommendation would be, whenever there is uncertainty involved, have a person double-check the results and confirm the year. This is the problem with two-digit years – there is always a chance you guessed the wrong year.

Related Articles

  • C# – Convert DateTime to string
  • C# – Get the current date and time
  • The JSON value could not be converted to System.DateTime
  • C# – TimeZoneInfo with current UTC offset
  • C# – How to use TimeZoneInfo
C# - Parsing a DateTime from a string | makolyte (2024)

FAQs

How to parse DateTime from string 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”.

What is parsing how do you parse a date time string? ›

The Date.parse() static method parses a string representation of a date, and returns the date's timestamp. Only the date time string format is explicitly specified to be supported. Other formats are implementation-defined and may not work across all browsers.

How to read date time in C#? ›

DateTime in C#
  1. // From DateTime create the Date and Time.
  2. DateTime DOB= new DateTime(19, 56, 8, 12, 8, 12, 23);
  3. // From String creation of DateTime.
  4. string DateString= "8/12/1956 7:10:24 AM";
  5. DateTime dateFromString =
  6. DateTime.Parse(DateString, System.Globalization.CultureInfo.InvariantCulture);

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

ParseExact(String, String, IFormatProvider)

The dateString variable receives the string representation of date and time 15/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 .

How to parse a string to DateTime? ›

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 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 use DateTime.ParseExact in C#? ›

try { dateValue = DateTime. ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles. None); Console. WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, dateValue.

How to format DateTime 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.

What is the format for parse date to date? ›

The most reliable way to parse a date string in JavaScript is to use the ISO date format. ISO format can be in the form of YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS . However, there's a caveat: JavaScript may interpret the input string as either UTC or local time, depending on the browser vendor and version.

What are the methods of date time in C#? ›

Working with Date and Time in C#
  • DateTime newDate = new DateTime(year, month, day); DateTime newDate = new DateTime(year, month, day, hour, munite, second);
  • DateTime currentDateTime = DateTime.Now; //returns current date and time. ...
  • // Calculate age with TimeSpan struct.
Aug 30, 2020

How to get date and time as string in C#? ›

To do this, you use the "MM/yyyy" format string. The format string uses the current culture's date separator. Getting a string that contains the date and time in a specific format. For example, the "MM/dd/yyyyHH:mm" format string displays the date and time string in a fixed format such as "19//03//2013 18:06".

How to get DateTime from timestamp in C#? ›

For conversion of Timestamp to date in C# epochs play a vital role and that in turn have different syntax and conversion process represented as follows:
  1. Select a conventional date of choice.
  2. Then try to make and convert the System. ...
  3. Add the required number of seconds for the UNIX timestamp to convert.
Apr 1, 2023

How to Parse string to DateTime in C#? ›

Methods to Convert String to DateTime in C#
  1. Parse()
  2. ParseExact()
  3. TryParse()
  4. TryParseExact()
Jun 11, 2024

What is the difference between Parse and ParseExact? ›

Differences between Parse and ParseExact:

Parse : More flexible as it uses culture-specific information to interpret the date and time string. It will use the format that's most common for the current culture. ParseExact : Requires you to specify exactly how the string should be parsed using a format string.

What is the difference between convert ToDateTime and DateTime Parse? ›

Difference between Parse() and ConvertToDateTime()

If the string value is null, Parse() throws an Exception while ConvertToDateTime() returns DateTime. MinValue.

How to convert string to datetime2 in C#? ›

You can similarly use DateTime. ParseExact(yourDateTimeString, "yyyy-MM-dd HH:mm:ss. fff", CultureInfo. InvariantCulture) in the c# code and pass the value natively as a datetime2 parameter, avoiding the need for SQL Server to parse the string.

How to separate time from DateTime in C#? ›

The best way to get time from DateTime in C# is to:
  1. Access the TimeOfDay property of DateTime to get TimeSpan value for your DateTime .
  2. Access the Hours property of TimeSpan to get the hour of the DateTime .
  3. Access the Minutes property of TimeSpan to get the minutes of the DateTime .
Jun 19, 2022

How to split data from string in C#? ›

Using the String. Split()
  1. string sentence = "Hello, world! Welcome to C# programming.";
  2. char separator = ''; // Space character.
  3. string [] words = sentence. Split(separator); // returned array.
Aug 29, 2023

How to return DateTime to string in C#? ›

The ToString() method of the DateTime class is used to convert a DateTime date object to string format. The method takes a date format string that specifies the required string representation.

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5373

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.