Example

using System;
using System.Globalization;
namespace Samples
{
  public class DateTimeStylesSample
  {
    public static void Main()
    {
      IFormatProvider f = new CultureInfo("en-AU", true);
      string s = String.Format("\t  {0}  ",
                     DateTime.Now.ToString(f));
      DateTime d = DateTime.Parse(s, f,
                   DateTimeStyles.AllowWhiteSpaces);
      Console.WriteLine("String: {0} yields date {1}", s, d);
      d = DateTime.Parse(s, f,
                   DateTimeStyles.AllowLeadingWhite |
                   DateTimeStyles.AllowInnerWhite |
                   DateTimeStyles.AllowTrailingWhite);
      Console.WriteLine("String: {0} yields date {1}", s, d);
      s = String.Format("  {0}  \t",
                     DateTime.Now.ToString("T", f));
      d = DateTime.Parse(s, f,
                   DateTimeStyles.NoCurrentDateDefault);
      Console.WriteLine("String: {0} yields date {1}", s, d);
    }
  }
}
The output ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.