Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

What is the DateTime TryParse method?

Nouman Abbasi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The DateTime TryParse method converts the string representation of a date and time to a DateTime object and returns true if the conversion was successful and false if otherwise.

Syntax

TryParse has four overloads:

  1. TryParse(ReadOnlySpan, IFormatProvider, DateTimeStyles, DateTime)
  2. TryParse(String, DateTime)
  3. TryParse(String, IFormatProvider, DateTimeStyles, DateTime)
  4. TryParse(ReadOnlySpan, DateTime)

This shot will cover the most commonly used one, TryParse(String, DateTime).

TryParse(String, DateTime)

This method converts the string representation of a date and time to the equivalent DateTime object. The method returns True if the conversion was successful and false if otherwise.

public static bool TryParse (string? s, out DateTime? result);
  • s: A string containing a date and time to convert.
  • result: When this method returns, result contains the DateTime value equivalent to the date and time contained in s if the conversion succeeded and MinValue if the conversion failed.

Returns true if the string s was converted successfully; otherwise, false.

The DateTime.TryParse(String, DateTime) method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails.

Code

The following example passes several date and time strings to the DateTime.TryParse(String, DateTime) method:

using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime dateValue; // to store the results
// Example 1
string dateString1 = "05/01/2020 14:57:32.8";
if (DateTime.TryParse(dateString1, out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString1, dateValue);
else
Console.WriteLine("Unable to parse '{0}'.", dateString1);
// Example 2
string dateString2 = "1 May 2020 2:57:32.8 PM";
if (DateTime.TryParse(dateString2, out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString2, dateValue);
else
Console.WriteLine("Unable to parse '{0}'.", dateString2);
// Example 3
string dateString3 = "not a date";
if (DateTime.TryParse(dateString3, out dateValue))
Console.WriteLine("Converted '{0}' to {1}.", dateString3, dateValue);
else
Console.WriteLine("Unable to parse '{0}'.", dateString3);
}
}
// The examples above display the following:
// Converted '05/01/2020 14:57:32.8' to 5/1/2020 2:57:32 PM.
// Converted '1 May 2020 2:57:32.8 PM' to 5/1/2020 2:57:32 PM .
// Unable to parse 'not a date'.

RELATED TAGS

c#

CONTRIBUTOR

Nouman Abbasi
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring