More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Sunday, 21 June 2009 10:27:36 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 522

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;



Edited by user Sunday, 21 June 2009 10:31:53 AM(UTC)  | Reason: Not specified

Sponsor
hong  
#2 Posted : Sunday, 21 June 2009 10:34:41 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 23/11/2008(UTC)
Posts: 522

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;

Rss Feed  Atom Feed
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.