Home  |  Delphi .net Home  |  System.DateTime  |  ToString Method
ToString  
Method  
Converts the current DateTime to a string
DateTime Structure
System NameSpace
CF1.  Function ToString ( ) : String ;
CF2.  Function ToString ( Formatting : String; ) : String ;
CF3.  Function ToString ( Formatting:StringFormatting : String; FormatProvider : IFormatProvider; ) : String ;
CF4.  Function ToString ( FormatProvider : IFormatProvider; ) : String;
CF : Methods with this mark are Compact Framework Compatible
Description
The ToString method concerts the current DateTime value into a string representation.
 
By default, with no parameters, it is equivalent to ToString('G') - 'General' formatting is carried.
 
The Formatting string (or array of strings) provides the desired formatting 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 places
fff Fraction of a second to 3 decimal places
ffff Fraction of a second to 4 decimal places
fffff Fraction of a second to 5 decimal places
ffffff Fraction of a second to 6 decimal places
fffffff Fraction of a second to 7 decimal places
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

 
The FormatProvider parameter determines the parsing rules and is beyond the scope of this article.
Notes
ToString without parameters is a method inherited from the base System.Object (TObject) class. It provides a ready method for classes to convert their content into a string format.
Microsoft MSDN Links
System
System.DateTime
 
 
Examples of single character and multiple character formatting
program Project1;
{$APPTYPE CONSOLE}

var
  when      : DateTime;

begin
  // 8:03:02 on 3rd September 2004
  when      := DateTime.Create(2004, 9, 3, 8, 3, 2, 123);

  Console.WriteLine('1. Single character formatting :');
  Console.WriteLine;

  Console.WriteLine('d : Short Date     : ' + when.ToString('d'));
  Console.WriteLine('D : Long Date      : ' + when.ToString('D'));
  Console.WriteLine('t : Short Time     : ' + when.ToString('t'));
  Console.WriteLine('T : Long Time      : ' + when.ToString('T'));
  Console.WriteLine('f : Full date      : ' + when.ToString('f'));
  Console.WriteLine('F : Long full date : ' + when.ToString('F'));
  Console.WriteLine('g : General        : ' + when.ToString('g'));
  Console.WriteLine('G : Long general   : ' + when.ToString('G'));
  Console.WriteLine('M : Month          : ' + when.ToString('M'));
  Console.WriteLine('R : RFC1123        : ' + when.ToString('R'));
  Console.WriteLine('s : Sortable       : ' + when.ToString('s'));
  Console.WriteLine('u : Universal sort : ' + when.ToString('u'));
  Console.WriteLine('Y : Year           : ' + when.ToString('Y'));

  Console.WriteLine;
  Console.WriteLine('2. Multiple character formatting :');
  Console.WriteLine;

  Console.WriteLine('d/M/y              : ' + when.ToString('d/M/y'));
  Console.WriteLine('dd MM yy           : ' + when.ToString('dd MM yy'));
  Console.WriteLine('ddd MMM yyyy       : ' + when.ToString('ddd MMM yyyy'));
  Console.WriteLine('dddd MMMM yyyy gg  : ' + when.ToString('dddd MMMM yyyy gg'));
  Console.WriteLine('h:m:s              : ' + when.ToString('h:m:s'));
  Console.WriteLine('hh:mm:ss           : ' + when.ToString('hh:mm:ss'));
  Console.WriteLine('hh:mm:ss           : ' + when.ToString('hh:mm:ss'));
  Console.WriteLine('hh:mm:ss.f         : ' + when.ToString('hh:mm:ss.f'));
  Console.WriteLine('hh:mm:ss.ff        : ' + when.ToString('hh:mm:ss.ff'));
  Console.WriteLine('hh:mm:ss.fff       : ' + when.ToString('hh:mm:ss.fff'));
  Console.WriteLine('hh:mm:ss.ffff      : ' + when.ToString('hh:mm:ss.ffff'));
  Console.WriteLine('hh:mm:ss.fffff     : ' + when.ToString('hh:mm:ss.fffff'));
  Console.WriteLine('hh:mm:ss.ffffff    : ' + when.ToString('hh:mm:ss.ffffff'));
  Console.WriteLine('hh:mm:ss.fffffff   : ' + when.ToString('hh:mm:ss.fffffff'));
  Console.WriteLine('hh:mm:ss t         : ' + when.ToString('hh:mm:ss t'));
  Console.WriteLine('hh:mm:ss tt        : ' + when.ToString('hh:mm:ss tt'));
  Console.WriteLine('hh:mm:ss z         : ' + when.ToString('hh:mm:ss z'));
  Console.WriteLine('hh:mm:ss zz        : ' + when.ToString('hh:mm:ss zz'));
  Console.WriteLine('hh:mm:ss zzz       : ' + when.ToString('hh:mm:ss zzz'));

  Console.ReadLine;
end.
Show full unit code
  1. Single character formatting :
  
  d : Short Date     : 03/09/2004
  D : Long Date      : 03 September 2004
  t : Short Time     : 08:03
  T : Long Time      : 08:03:02
  f : Full date      : 03 September 2004 08:03
  F : Long full date : 03 September 2004 08:03:02
  g : General        : 03/09/2004 08:03
  G : Long general   : 03/09/2004 08:03:02
  M : Month          : 03 September
  R : RFC1123        : Fri, 03 Sep 2004 08:03:02 GMT
  s : Sortable       : 2004-09-03T08:03:02
  u : Universal sort : 2004-09-03 08:03:02Z
  Y : Year           : September 2004
  
  2. Multiple character formatting :
  
  d/M/y              : 3/9/4
  dd MM yy           : 03 09 04
  ddd MMM yyyy       : Fri Sep 2004
  dddd MMMM yyyy gg  : Friday September 2004 A.D.
  h:m:s              : 8:3:2
  hh:mm:ss           : 08:03:02
  hh:mm:ss           : 08:03:02
  hh:mm:ss.f         : 08:03:02.1
  hh:mm:ss.ff        : 08:03:02.12
  hh:mm:ss.fff       : 08:03:02.123
  hh:mm:ss.ffff      : 08:03:02.1230
  hh:mm:ss.fffff     : 08:03:02.12300
  hh:mm:ss.ffffff    : 08:03:02.123000
  hh:mm:ss.fffffff   : 08:03:02.1230000
  hh:mm:ss t         : 08:03:02 A
  hh:mm:ss tt        : 08:03:02 AM
  hh:mm:ss z         : 08:03:02 +1
  hh:mm:ss zz        : 08:03:02 +01
  hh:mm:ss zzz       : 08:03:02 +01:00
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author