w3resource

C#: String representation of a date and time to its DateTime equivalent

C# Sharp DateTime: Exercise-42 with Solution

Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent. This is done using the specified format, culture-specific format information, and style.

Sample Solution:-

C# Sharp Code:

using System;
using System.Globalization;

public class Example42
{
   public static void Main()
   {
      string dateString;
      CultureInfo culture;
      DateTimeStyles styles;
      DateTime dateResult;

      // Parse a date and time with no styles.
      dateString = "05/06/2016 10:00 AM";
      culture = CultureInfo.CreateSpecificCulture("de-DE");
      styles = DateTimeStyles.None;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.", 
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", 
                           dateString);

      // Parse the same date and time with the AssumeLocal style.
      styles = DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.", 
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Parse a date and time that is assumed to be local.
      // This time is five hours behind UTC. The local system's time zone is 
      // eight hours behind UTC.
      dateString = "2016/05/06T10:00:00-5:00";
      styles = DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.", 
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Attempt to convert a string in improper ISO 8601 format.
      dateString = "05/06/2016T10:00:00-5:00";
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.", 
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

      // Assume a date and time string formatted for the fr-BE culture is the local 
      // time and convert it to UTC.
      dateString = "2015-05-06 10:00";
      culture = CultureInfo.CreateSpecificCulture("fr-BE");
      styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
      if (DateTime.TryParse(dateString, culture, styles, out dateResult))
         Console.WriteLine("{0} converted to {1} {2}.", 
                           dateString, dateResult, dateResult.Kind);
      else
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString);

   }
}

Sample Output:

05/06/2016 10:00 AM converted to 6/5/2016 10:00:00 AM Unspecified.                                            
05/06/2016 10:00 AM converted to 6/5/2016 10:00:00 AM Local.                                                  
2016/05/06T10:00:00-5:00 converted to 5/6/2016 8:30:00 PM Local.                                              
Unable to convert 05/06/2016T10:00:00-5:00 to a date and time.                                                
2015-05-06 10:00 converted to 5/6/2015 4:30:00 AM Utc.

Flowchart :

Flowchart: C# Sharp Exercises - String representation of a date and time to its DateTime equivalent

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to convert the specified string representation of a date and time to its DateTime equivalent.
Next: Write a program in C# Sharp to check whether the given year, month and day are the current or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook