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; // Variable to store the date string to be parsed.
      CultureInfo culture; // CultureInfo used for parsing date string.
      DateTimeStyles styles; // DateTimeStyles used for parsing.
      DateTime dateResult; // Variable to store the parsed DateTime.

      // Parse a date and time with no styles.
      dateString = "05/06/2016 10:00 AM"; // Date string to be parsed.
      culture = CultureInfo.CreateSpecificCulture("de-DE"); // German culture for parsing.
      styles = DateTimeStyles.None; // No specific DateTimeStyles set.
      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; // DateTimeStyles to 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"; // Date string with UTC offset.
      styles = DateTimeStyles.AssumeLocal; // AssumeLocal DateTimeStyles.
      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"; // Improperly formatted date string.
      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"; // Date string for fr-BE culture.
      culture = CultureInfo.CreateSpecificCulture("fr-BE"); // French culture.
      styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal; // Combine DateTimeStyles.
      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.