w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Convert string representation of a date and time to its DateTime equivalent

Secondary Nav

C# Sharp DateTime : Exercise-41 with Solution

Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent.

Expected Output :

Attempting to parse strings using en-US culture.                                 
  Converted '05/01/2016 14:57:32.8' to 5/1/2016 2:57:32 PM (Unspecified).        
  Converted '2016-05-01 14:57:32.8' to 5/1/2016 2:57:32 PM (Unspecified).        
  Converted '2016-05-01T14:57:32.8375298-04:00' to 5/2/2016 12:27:32 AM (Local). 
  Converted '5/01/2015' to 5/1/2015 12:00:00 AM (Unspecified).                   
  Converted '5/01/2015 14:57:32.80 -07:00' to 5/2/2015 3:27:32 AM (Local).       
  Converted '1 May 2015 2:57:32.8 PM' to 5/1/2015 2:57:32 PM (Unspecified).      
  Unable to parse '16-05-2016 1:00:32 PM'.                                       
  Unable to parse 'Fri, 15 May 2016 20:10:57 GMT'.          

C# Sharp Code:

using System;
using System.Globalization;

public class Example41
{
   public static void Main()
   {
      string[] dateStrings = {"05/01/2016 14:57:32.8", "2016-05-01 14:57:32.8",
                              "2016-05-01T14:57:32.8375298-04:00", "5/01/2015",
                              "5/01/2015 14:57:32.80 -07:00", 
                              "1 May 2015 2:57:32.8 PM", "16-05-2016 1:00:32 PM", 
                              "Fri, 15 May 2016 20:10:57 GMT" };
      DateTime dateValue;

      Console.WriteLine("Attempting to parse strings using {0} culture.", 
                        CultureInfo.CurrentCulture.Name);
      foreach (string dateString in dateStrings)
      {
         if (DateTime.TryParse(dateString, out dateValue)) 
            Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString, 
                              dateValue, dateValue.Kind);
         else
            Console.WriteLine("  Unable to parse '{0}'.", dateString);
      }
   }
}

C# Sharp Practice online:

using System;
using System.Globalization;

public class Example41
{
   public static void Main()
   {
      string[] dateStrings = {"05/01/2016 14:57:32.8", "2016-05-01 14:57:32.8",
                              "2016-05-01T14:57:32.8375298-04:00", "5/01/2015",
                              "5/01/2015 14:57:32.80 -07:00", 
                              "1 May 2015 2:57:32.8 PM", "16-05-2016 1:00:32 PM", 
                              "Fri, 15 May 2016 20:10:57 GMT" };
      DateTime dateValue;

      Console.WriteLine("Attempting to parse strings using {0} culture.", 
                        CultureInfo.CurrentCulture.Name);
      foreach (string dateString in dateStrings)
      {
         if (DateTime.TryParse(dateString, out dateValue)) 
            Console.WriteLine("  Converted '{0}' to {1} ({2}).", dateString, 
                              dateValue, dateValue.Kind);
         else
            Console.WriteLine("  Unable to parse '{0}'.", dateString);
      }
   }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program