Home  |  Delphi .net Home  |  System.DateTime  |  ParseExact Method
  
ParseExact  
Method  
Converts a string representation of date and time into a DateTime value
DateTime Structure
System NameSpace
CF1.  Function ParseExact ( Value : String; Formatting : String; FormatProvider : IFormatProvider ) : DateTime; Static;
CF2.  Function ParseExact ( Value : String; Formatting : String; FormatProvider : IFormatProvider; Style : DateTimeStyles ) : DateTime; Static;
CF3.  Function ParseExact ( Value : String; Formatting : Array of String; FormatProvider : IFormatProvider; Style : DateTimeStyles ) : DateTime; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Parses the Value string representation of date and/or time into a DateTime value.
 
The Formatting string (or array of strings) provides the exact syntax of the date/time string. This formatting string may be of two types :
 
1. A single formatting style character :
 
Short Date, such as : 24/08/2004
Long date, such as 24 August 2004
Short time, such as 12:23
Long time, such as 12:23:45
Full datetime, such as 24 August 2004 12:23
Full datetime, such as 24 August 2004 12:23:45
General datetime, such as 24/08/2004 12:23
General datetime, such as 24/08/2004 12:23:45
M or m Month, such as 24 August
R or r Culture independant, such as Tue 24 August 2004 12:23:45 GMT
Sortable datetime, such as 2004-08-24T12:23:45
Universal sortable datetime
Y or y Year plus month, such as August 2004

 
2. A formatting string
 
Use a combination of the following to build the format you want (see the example code). If, for example, you just want to use 'd' on its onw, then you must prefix it by % to distinguish from the single character 'd'.
 
Numeric day of the month with no leading 0
dd Numeric day of the month with leading 0
ddd Abbreviated day of the month
dddd Full name of the day of the week
Numeric Month with no leading 0
MM Numeric Month number with leading 0
MMM Abbreviated month name
MMMM Full month name
Numeric year without century and no leading 0
yy Numeric year without century andleading 0
yyyy Numeric year in 4 digits
gg Period or era
Numeric hour with no leading 0
hh Numeric hour with leading 0
Numeric 24 hour clock hour with no leading 0
HH Numeric 25=4 hour clock hour with leading 0
Numeric minute with no leading 0
mm Numeric minute with leading 0
Numeric second with no leading 0
ss Numeric second with leading 0
Fraction of a second to 1 decimal place
ff Fraction of a second to 2 decimal place
fff Fraction of a second to 3 decimal place
ffff Fraction of a second to 4 decimal place
fffff Fraction of a second to 5 decimal place
ffffff Fraction of a second to 6 decimal place
fffffff Fraction of a second to 7 decimal place
The first am/pm designator character
tt The full am/pm designator characters
The time zone offest (+/- hour) no leading 0
zz The time zone offest (+/- hour) with leading 0
zzz The time zone offest (+/- hour) with leading 0's
Default time separator
Default date separator

 
Omitting the date component defaults the date to the current date.
 
The FormatProvider parameter determines the parsing rules and is beyond the scope of this article. (See the example for providing this parameter).
 
The optional Style parameter determines the allowed characters, such as blanks, in the Value.
Notes
Static methods are not methods of an object - they are simply class functions or procedures available at any time.
References
DateTimeStyles
Microsoft MSDN Links
System
System.DateTime
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Examples of single character and multiple character format
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  when : DateTime;
  formatProvider : IFormatProvider;

begin
  Console.WriteLine('This example uses UK datetime formatting');
  Console.WriteLine;

  formatProvider := System.Globalization.CultureInfo.Create('en-GB');

  when := System.DateTime.ParseExact('24/06/2004 11:12:13', 'G',
                                     formatProvider);
  Console.WriteLine('Parsed value = {0:F}', when);

  when := System.DateTime.ParseExact('24/06/04', 'dd/MM/yy',
                                     formatProvider);
  Console.WriteLine('Parsed value = {0:F}', when);

  when := System.DateTime.ParseExact('24 June 2004', 'dd MMMM yyyy',
                                     formatProvider);
  Console.WriteLine('Parsed value = {0:F}', when);

  Console.ReadLine;
end.
   This example uses UK datetime formatting
  
   Parsed value = 24 June 2004 11:12:13
   Parsed value = 24 June 2004 00:00:00
   Parsed value = 24 June 2004 00:00:00
Using DateTimeStyles
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  when           : DateTime;
  formatProvider : IFormatProvider;

begin
  Console.WriteLine('This example uses UK datetime formatting');
  Console.WriteLine;

  formatProvider := System.Globalization.CultureInfo.Create('en-GB');

  Console.WriteLine('Allow leading, inner and trailing blanks');
  Console.WriteLine;
  Console.WriteLine('String       = 24 / 06 / 2004 11 : 12 : 13 ');

  when := System.DateTime.ParseExact(' 24 / 06 / 2004 11 : 12 : 13 ', 'G',
                 formatProvider,
                 System.Globalization.DateTimeStyles.AllowWhiteSpaces);

  Console.WriteLine('Parsed value = {0:F}', when);

  Console.ReadLine;
end.
   This example uses UK datetime formatting
  
   Allow leading, inner and trailing blanks
  
   String       = 24 / 06 / 2004 11 : 12 : 13
   Parsed value = 24 June 2004 11:12:13
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author