DateTime.Parse Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the specified string representation of a date and time to its DateTime equivalent.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Parse ( _
    s As String _
) As DateTime
public static DateTime Parse(
    string s
)

Parameters

  • s
    Type: System.String
    A string that contains a date and time to convert.

Return Value

Type: System.DateTime
An object that is equivalent to the date and time contained in the s parameter.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

FormatException

s does not contain a valid string representation of a date and time.

Remarks

The DateTime.Parse(String) method tries to convert the string representation of a date and time value to its DateTime equivalent. The string to be parsed can take any of the following forms:

  • A string with a date and a time component.

  • A string with a date but no time component.

  • A string with a time but no date component.

  • A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:

    2008-11-01T19:35:00.0000000Z

    2008-11-01T19:35:00.0000000-07:00

  • A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:

    Sat, 01 Nov 2008 19:35:00 GMT

  • A string that includes the date and time along with time zone offset information. For example:

    03/01/2009 05:42:00 -5:00

This method attempts to parse s completely and avoid throwing a FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes 12:00 midnight. If s contains only a time and no date, this method assumes the current date. If s includes a date component with a two-digit year, it is converted to a year in the current culture's current calendar based on the value of the Calendar.TwoDigitYearMax property. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ('#', U+0023), and can be trailed with one or more NULL characters (U+0000).

Important noteImportant Note:

Because the string representation of a date and time must conform to a recognized pattern, applications should always use exception handling when calling the Parse(String) method to parse user input. Alternatively, you can call the DateTime.TryParse(String, DateTime%) method to parse a date and time string and return a value that indicates whether the parse operation succeeded.

The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture. The s parameter must contain the representation of a date and time in any of the formats defined by the current culture's DateTimeFormatInfo object.

Important noteImportant Note:

Because the Parse(String) method tries to parse the string representation of a date and time using the formatting rules of the current culture, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use the DateTime.Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier.

In most cases, the Parse(String) method returns a DateTime value whose Kind property is DateTimeKind.Unspecified. However, if the string to be parsed contains time zone information as defined by ISO 8601 or if it includes the older GMT designator, the Parse(String) method performs any necessary time conversion and returns a DateTime value whose date and time reflects the local time and whose Kind property is DateTimeKind.Local. The following example illustrates these string representations.

Dim dateStrings() As String = {"2008-05-01T07:34:42-5:00", _
                               "2008-05-01 7:34:42Z", _
                               "Thu, 01 May 2008 07:34:42 GMT"}

For Each dateString As String In dateStrings
   Dim convertedDate As Date = Date.Parse(dateString)
   outputBlock.Text += String.Format("Converted {0} to {1} time {2}.", _
                     dateString, _
                     convertedDate.Kind.ToString(), _
                     convertedDate) & vbCrLf
Next
' These calls to the DateTime.Parse method display the following output:
'    Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM.
'    Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM.
'    Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM.                                             
string[] dateStrings = {"2008-05-01T07:34:42-5:00", 
                        "2008-05-01 7:34:42Z", 
                        "Thu, 01 May 2008 07:34:42 GMT"};
foreach (string dateString in dateStrings)
{
   DateTime convertedDate = DateTime.Parse(dateString);
   outputBlock.Text += String.Format("Converted {0} to {1} time {2}.",
                     dateString,
                     convertedDate.Kind.ToString(),
                     convertedDate) + "\n";
}
// These calls to the DateTime.Parse method display the following output:
//    Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM.
//    Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM.
//    Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM.                                             

Examples

The following example demonstrates the Parse(String) method. It parses the string representation of several date and time values using the formatting conventions of the en-US culture, which is the current thread culture of the computer used to produce the example output. It handles the FormatException that is thrown when the method tries to parse the string representation of a date and time using some other culture's formatting conventions. It also shows how to successfully parse a date and time value that does not use the formatting conventions of the current thread culture.

Imports System.Globalization

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Assume the current culture is en-US. 
      ' The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.

      ' Use standard en-US date and time value
      Dim dateValue As Date
      Dim dateString As String = "2/16/2008 12:15:12 PM"
      Try
         dateValue = Date.Parse(dateString)
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) & vbCrLf
      End Try

      ' Reverse month and day to conform to the fr-FR culture.
      ' The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
      dateString = "16/02/2008 12:15:12"
      Try
         dateValue = Date.Parse(dateString)
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) & vbCrLf
      End Try

      ' Call another overload of Parse to successfully convert string
      ' formatted according to conventions of fr-FR culture.      
      Try
         dateValue = Date.Parse(dateString, New CultureInfo("fr-FR"))
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) & vbCrLf
      End Try

      ' Parse string with date but no time component.
      dateString = "2/16/2008"
      Try
         dateValue = Date.Parse(dateString)
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) & vbCrLf
      End Try
   End Sub
End Class
' The example displays the following output:
'       '2/16/2008 12:15:12 PM' converted to 2/16/2008 12:15:12 PM.
'       Unable to convert '16/02/2008 12:15:12'.
'       '16/02/2008 12:15:12' converted to 2/16/2008 12:15:12 PM.
'       '2/16/2008' converted to 2/16/2008 12:00:00 AM.
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Assume the current culture is en-US. 
      // The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.

      // Use standard en-US date and time value
      DateTime dateValue;
      string dateString = "2/16/2008 12:15:12 PM";
      try
      {
         dateValue = DateTime.Parse(dateString);
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) + "\n";
      }

      // Reverse month and day to conform to the fr-FR culture.
      // The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
      dateString = "16/02/2008 12:15:12";
      try
      {
         dateValue = DateTime.Parse(dateString);
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) + "\n";
      }

      // Call another overload of Parse to successfully convert string
      // formatted according to conventions of fr-FR culture.      
      try
      {
         dateValue = DateTime.Parse(dateString, new CultureInfo("fr-FR"));
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) + "\n";
      }

      // Parse string with date but no time component.
      dateString = "2/16/2008";
      try
      {
         dateValue = DateTime.Parse(dateString);
         outputBlock.Text += String.Format("'{0}' converted to {1}.", dateString, dateValue) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert '{0}'.", dateString) + "\n";
      }
   }
}
// The example displays the following output:
//       '2/16/2008 12:15:12 PM' converted to 2/16/2008 12:15:12 PM.
//       Unable to convert '16/02/2008 12:15:12'.
//       '16/02/2008 12:15:12' converted to 2/16/2008 12:15:12 PM.
//       '2/16/2008' converted to 2/16/2008 12:00:00 AM.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.