Parse date and time value, and allow all white space. : DateTimeOffset Parse « Date Time « C# / C Sharp






Parse date and time value, and allow all white space.

Parse date and time value, and allow all white space.
 

using System;
using System.Globalization;
public class Test
{
   public static void Main()
   {  
        string dateString, format;  
        DateTimeOffset result;
        CultureInfo provider = CultureInfo.InvariantCulture;

        dateString = " 06/15/   2008  15:15    -05:00";
        format = "MM/dd/yyyy H:mm zzz";
        try
        {
           result = DateTimeOffset.ParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces);
           Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
        }   
        catch (FormatException)
        {
           Console.WriteLine("'{0}' is not in the correct format.", dateString);
        } 
   }
}        

   
  








Related examples in the same category

1.Converts string to DateTimeOffset: String with date only(05/01/2008)Converts string to DateTimeOffset: String with date only(05/01/2008)
2.Converts string to DateTimeOffset: String with time only(11:36 PM)Converts string to DateTimeOffset: String with time only(11:36 PM)
3.Converts string to DateTimeOffset: String with date and offset (05/01/2008 +1:00)Converts string to DateTimeOffset: String with date and offset (05/01/2008 +1:00)
4.Converts string to DateTimeOffset: String with day abbreviation(Thu May 01, 2008)Converts string to DateTimeOffset: String with day abbreviation(Thu May 01, 2008)
5.Converts string to DateTimeOffset using the specified culture-specific format information.Converts string to DateTimeOffset using the specified culture-specific format information.
6.Converts string to DateTimeOffset:"15/09/07 08:45:00 +1:00"Converts string to DateTimeOffset:
7.Converts string to DateTimeOffset:"mar. 1 janvier 2008 1:00:00 +1:00"Converts string to DateTimeOffset:
8.Converts string to DateTimeOffset using the specified culture-specific format information and formatting style.Converts string to DateTimeOffset using the specified culture-specific format information and formatting style.
9.Parse date-only value with invariant culture.Parse date-only value with invariant culture.
10.Parse date-only value without leading zero in month using "d" format.Parse date-only value without leading zero in month using
11.Parse date and time with custom specifier.Parse date and time with custom specifier.
12.Parse date and time with offset without offset//s minutes.Parse date and time with offset without offset//s minutes.
13.Parse date-only value with invariant culture and assume time is UTC.Parse date-only value with invariant culture and assume time is UTC.
14.Parse date-only value with leading white space.Parse date-only value with leading white space.
15.Parse date and time and convert to UTC.Parse date and time and convert to UTC.
16.Tries to converts a string to DateTimeOffset(String with date only)
17.Tries to converts a string to DateTimeOffset(String with time only)
18.Tries to converts a string to DateTimeOffset(String with date and offset )
19.Tries to converts a string to DateTimeOffset(String with day abbreviation)
20.Tries to converts a string to DateTimeOffset(String with date, time with AM/PM designator, and offset)