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 :
d | Short Date, such as : 24/08/2004 |
D | Long date, such as 24 August 2004 |
t | Short time, such as 12:23 |
T | Long time, such as 12:23:45 |
f | Full datetime, such as 24 August 2004 12:23 |
F | Full datetime, such as 24 August 2004 12:23:45 |
g | General datetime, such as 24/08/2004 12:23 |
G | 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 |
s | Sortable datetime, such as 2004-08-24T12:23:45 |
u | 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'.
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 |
M | Numeric Month with no leading 0 |
MM | Numeric Month number with leading 0 |
MMM | Abbreviated month name |
MMMM | Full month name |
y | 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 |
h | Numeric hour with no leading 0 |
hh | Numeric hour with leading 0 |
H | Numeric 24 hour clock hour with no leading 0 |
HH | Numeric 25=4 hour clock hour with leading 0 |
m | Numeric minute with no leading 0 |
mm | Numeric minute with leading 0 |
s | Numeric second with no leading 0 |
ss | Numeric second with leading 0 |
f | 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 |
t | The first am/pm designator character |
tt | The full am/pm designator characters |
z | 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
|
|
|
Examples of single character and multiple character format |
// Full Unit code. // ------------------------------------------------------------- // Create a new WinForm application, double click the form to // create an OnLoad event, and then replace the WinForm unit // with this text. unit WinForm; interface uses System.Drawing, System.Collections, System.ComponentModel, System.Windows.Forms, System.Data; type TWinForm = class(System.Windows.Forms.Form) \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign strict private /// /// Required designer variable. /// Components: System.ComponentModel.Container; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// procedure InitializeComponent; procedure TWinForm_Load(sender: System.Object; e: System.EventArgs); {ENDREGION} strict protected /// /// Clean up any resources being used. /// procedure Dispose(Disposing: Boolean); override; private { Private Declarations } public constructor Create; end; [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))] implementation \{REGION 'Windows Form Designer generated code'\} /// /// Required method for Designer support -- do not modify /// the contents of this method with the code editor. /// procedure TWinForm.InitializeComponent; begin // // TWinForm // Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13); Self.ClientSize := System.Drawing.Size.Create(292, 266); Self.Name := 'TWinForm'; Self.Text := 'WinForm'; Include(Self.Load, Self.TWinForm_Load); end; {ENDREGION} procedure TWinForm.Dispose(Disposing: Boolean); begin if Disposing then begin if Components <> nil then Components.Dispose(); end; inherited Dispose(Disposing); end; constructor TWinForm.Create; begin inherited Create; // // Required for Windows Form Designer support // InitializeComponent; // // TODO: Add any constructor code after InitializeComponent call // end; procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs); 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. end. | Hide full unit code | 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.
| Show full unit code | 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
|
|
|
|