More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

Converts a date or time or date+time string into a TDateTime value in Delphi Options · View
hong
Posted: Sunday, June 21, 2009 2:27:36 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 335
Points: 711
Location: Australia
Converts a date or time or date+time string into a TDateTime value in Delphi

StrToDateTime Converts a date+time string into a TDateTime value
1 function StrToDateTime ( const DateTime : string ) : TDateTime;
2 function StrToDateTime ( const DateTime : string; const FormatSettings : TFormatSettings ) : TDateTime;
Code:
  myDateTime := StrToDateTime('21/06/09 17');
  myDateTime := StrToDateTime('21/06/09 16:48:23');


String DateTime -> TDateTime myDateTime
-----------------------------------------
21/06/09 17 -> 21/06/09 17:00:00
21/06/09 16:48:23 -> 21/06/09 16:48:23

StrToDate Converts a date string into a TDateTime value
1 function StrToDate ( const Date : string ) : TDateTime;
2 function StrToDate ( const Date : string; const FormatSettings : TFormatSettings ) : TDateTime;

Code:
  myDate := StrToDate('18/06/09');
  myDate := StrToDate('18/06/09');



StrToTime Converts a time string into a TDateTime value

1 function StrToTime ( const Time : string ) : TDateTime;
2 function StrToTime ( const Time : string; const FormatSettings : TFormatSettings ) : TDateTime;

Code:
  myTime := StrToTime('9PM');
  myTime := StrToTime('21');
  myTime := StrToTime('21:01');
  myTime := StrToTime('21:01:01');



String Time -> TDateTime myTime
-----------------------------------------
9PM -> 21:00:00
21 -> 21:00:00
21:01 -> 21:01:00
21:01:01 -> 21:01:01


The following function ConvertToDateTime also converts a date string in DDMMYYYY format to a TDateTime
An exception is thrown on an invalid format.
Code:
function ConvertToDateTime(const strDate : String): TDateTime;
var
  strDay, strMonth, strYear: Word;
begin
  strDay := StrToInt(Copy(strDate, 1, 2));
  strMonth := StrToInt(Copy(strDate, 3, 2));
  strYear := StrToInt(Copy(strDate, 5, 4));
  Result := EncodeDate(strYear, strMonth, strDay);
end;

function IsValidDate(const strDate : String): Boolean;
begin
  Result := True;
  try
    ConvertToDateTime(strDate);
  except
    Result := False;
  end;
end;



Sponsor
Posted: Sunday, June 21, 2009 2:27:36 PM
hong
Posted: Sunday, June 21, 2009 2:34:41 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 335
Points: 711
Location: Australia
Date and Time Format in Delphi

y = Year last 2 digits
yy = Year last 2 digits
yyyy = Year as 4 digits
m = Month number no-leading 0
mm = Month number as 2 digits
mmm = Month using ShortDayNames (Jan)
mmmm = Month using LongDayNames (January)
d = Day number no-leading 0
dd = Day number as 2 digits
ddd = Day using ShortDayNames (Sun)
dddd = Day using LongDayNames (Sunday)
ddddd = Day in ShortDateFormat
dddddd = Day in LongDateFormat

c = Use ShortDateFormat + LongTimeFormat
h = Hour number no-leading 0
hh = Hour number as 2 digits
n = Minute number no-leading 0
nn = Minute number as 2 digits
s = Second number no-leading 0
ss = Second number as 2 digits
z = Milli-sec number no-leading 0s
zzz = Milli-sec number as 3 digits
t = Use ShortTimeFormat
tt = Use LongTimeFormat

am/pm = Use after h : gives 12 hours + am/pm
a/p = Use after h : gives 12 hours + a/p
ampm = As a/p but TimeAMString,TimePMString
/ = Substituted by DateSeparator value
: = Substituted by TimeSeparator value


DateSeparator = /
TimeSeparator = :
ShortDateFormat = dd/mm/yyyy
LongDateFormat = dd mmm yyyy
TimeAMString = AM
TimePMString = PM
ShortTimeFormat = hh:mm
LongTimeFormat = hh:mm:ss
ShortMonthNames = Jan Feb ...
LongMonthNames = January, February ...
ShortDayNames = Sun, Mon ...
LongDayNames = Sunday, Monday ...
TwoDigitYearCenturyWindow = 50

The default format in Australia is day/month/year, eg. '21/6/2009'.
MyDateTime : TDateTime = StrToDate('21/6/2009');
To change the date format from '6-21-2009' to '21/6/2009':
Code:
strInstallationDate := '6-21-2009';         
DateSeparator := '-';
          ShortDateFormat := 'd-m-yyyy';
          try
            MyDateTime := StrToDate(strInstallationDate);
            result := InstallationDate;
          except
            bValidDate := false;
          end;

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.067 seconds.