DelphiBasics
DateTimeToStr
Function
Converts TDateTime date and time values to a string SysUtils unit
1  function DateTimeToStr ( DateTime : TDateTime ) : string;
2  function DateTimeToStr ( DateTime : TDateTime; const FormatSettings : TFormatSettings ) : string;
Description
The DateTimeToStr function converts a TDateTime value DateTime into a formatted date and time string.
 
The string comprises :
 
Date in  ShortDateFormat
1 blank 
Time in  LongTimeFormat

 
See these two formatting variables for more details.
 
The date and time formats are also affected by the DateSeparator and TimeSeparator values.
 
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.
Notes
If the time is Midnight (00:00), then it is not stored in the string. Quite why is another matter.
Related commands
DateSeparator The character used to separate display date fields
DateTimeToString Rich formatting of a TDateTime variable into a string
LongTimeFormat Long version of the time to string format
ShortDateFormat Compact version of the date to string format
StrToDate Converts a date string into a TDateTime value
StrToDateTime Converts a date+time string into a TDateTime value
StrToTime Converts a time string into a TDateTime value
TFormatSettings A record for holding locale values for thread-safe functions
TimeSeparator The character used to separate display time fields
 Download this web site as a Windows program.


 
Example code : Converting two date/time values to strings
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the DateTimeToStr command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  myDate : TDateTime;

begin
  myDate := StrToDateTime('09/02/2002 12:00');
  ShowMessage('Middle of a day = '+DateTimeToStr(myDate));

  myDate := StrToDateTime('09/02/2002 00:00');
  ShowMessage('Start  of a day = '+DateTimeToStr(myDate));
end;
 
end.
Hide full unit code
   Middle of a day = 09/02/2002 12:00:00
   Start  of a day = 09/02/2002
 
 
Delphi Programming Neil Moffatt 2002 - 2014. All rights reserved.  |  Contact the author  |  Home Page