You are on page 1of 4

Mr Long Grade:

Subject:
11
Information Technology
Version:
Topic:
Beta
Date and Time
Videos

Mr L ong ON DATE AND TIME

Date and Time data types


Following data types are available in Delphi
• TDateTime is used to store a date and time value.
• TDate is used to store only a date value.
• TTime is used to store only a time value.

The following functions are used to obtain the computer system’s current date and time:
Function example Description
var dtTemp : TDateTime ; Declaration of a TDateTime variable.
dtTemp := Date ; Date function returns the current date, but the
time is recorded as the start of the day (00:00).
dtTemp := Time ; Time function returns the current time, but the
date is recorded as 1988/12/30.
dtTemp := Now ; Now function returns the current date and
current time.

Date and Time conversations


• When you want to display a date or time data type or when you receive a date or time in
a string format, the following functions can be used to convert between data types:
Function example Description
dtTemp := Date ; DateToStr function converts the given date
sToday := DateToStr( dtTemp ) ; argument into a returned string format.
dtTemp := Time ; TimeToStr function converts the given time
sTime := TimeToStr( dtTemp ) ; argument into a returned string format.
dtTemp := Now ; DateTimeToStr function converts the given date
sCurrent := DateTimeToStr( dtTemp ) ; time argument into a returned string format.
sInput := ‘2020/02/25’ ; StrToDate function converts the given string
dtTemp := StrToDate( sInput ) ; argument into a returned date format.
sInput := ’10:35:23’ ; StrToTime function converts the given string
dtTemp := StrToTime( sInput ) ; argument into a returned time format.

1
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Date and Time
Videos

• The EncodeDate functions accepts integer values for the year, month and day and
returns a date with those values. There are also an EncodeTime and EncodeDateTime
functions that take the relevant integer values to return a TTime and TDateTime value,
respectively.
Function example Description
dTemp will contain the TDate value of
dTemp := EncodeDate ( 2020 , 1, 14 ) ;
2020/01/14.

Converting into a specific format


• The FormatDateTime function converts a date and time variable into a type of string
based on a format using the following codes:
Unit of time Year Month Day Hour Minute Second Millisecond
Letter code Y M D H N S Z

Examples of letter codes


Format Description
yy TWO digit year (Example 19)
yyyy FOUR digit year (Example 2019)
mm TWO digit month (Example 02)
mmm Short month name (Example Feb)
mmmm Full month name (Example February)
dd TWO digit day (Example 15)
ddd Short day name (Example Mon)
dddd Full day name (Example Monday)
hh / mm / ss TWO digit hour / minutes / seconds

Function example
dtTemp := Now ;
sCurrent := FormatDateTime ( ‘dd mmm yyyy – hh:nn.ss’ , dtTemp ) ;
Description
A possible example of sCurrent’s string value could be:
21 Nov 2020 – 11:35.41

2
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Date and Time
Videos

isLeapYear function
• The isLeapYear Boolean function accepts an integer representing a year and
returns a true of that year is a leap year and false if it is not a leap year.
Function example
iYear := 2020 ;
if isLeapYear ( iYear ) = true then
showmessage(‘2020 was a leap year’)
else showmessage(‘2020 was NOT a leap year’) ;

Calculations with Dates


• When you subtract two dates, the result will be the number of days that occur
between the two dates.
• Adding an integer value to a date results in a new date after that many days (based
on the integer value) in the future. Subtracting an integer value results in a new date
before that many days.
Function example Description
rNum will contain the number of days between today
rNum := StrToDate(‘2020/2/14’) - Date ;
and Valentine’s Day in 2020 (14 February 2020).
dTemp will contain the date for two weeks from
dTemp := Date + 14 ;
today.
dTemp will contain the date for 30 days ago from
dTemp := Date - 30 ;
today.

Other Date and Time functions


• These functions require that you add DateUtils under uses.
Code Description
iYear := YearOf( dTemp ) ; YearOf returns the integer value of the year for a
given date.
iMonth := MonthOf( dTemp ) ; MonthOf returns the integer value of the month
for a given date.
iDay := DayOf( dTemp ) ; DayOf returns the integer value of the day for a
given date.
DaysInMonth returns the integer value of the
iDays := DaysInMonth( dTemp ) ; total number of days in the month of the given
date (Example 28, 30 or 31).
DaysInAMonth returns the integer value of the
iDays := DaysInAMonth( iYear, iMonth ) ; total number of days in the month of the given
year (Example 28, 30 or 31).
DaysBetween returns the integer value of the
iDays := DaysBetween( dTemp , Date ) ; total number of days between two given dates.
There are also a MonthsBetween and
YearsBetween functions.

3
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Date and Time
Videos

TDateTimePicker component
• The TDateTimePicker component is best used for input and output of a date or time
and can be found on the Win32 tab of the Tool Palette.
NOTE: The examples below will refer to a TDateTimePicker with name dtpInput.
• The component can only be used to display or allow the input of either a date or a
time.
When the component is used for a date. When the component is used for a time.

Properties Description
Date
The Date property contains the date indicated in
Example:
the component.
dtpInput.Date := StrToDate(‘2020/10/20’) ;
The DateFormat property is either dfShort
DateFormat
(2020/10/20) or dfLong (10 October 2020)
MaxDate and MinDate properties contain the
MaxDate and MinDate range of possible dates that can be stored or
selected in the component.
The Kind property is either dtkDate if the
component must be used to store or select dates
Kind
and dtkTime if the component must be used to
store or select times.
Time
The Time property contains the time indicated in
Example:
the component.
dtpInput.Time := StrToTime (’10:55:45’) ;

Additional Links:
• Youtube video playlist:
https://www.youtube.com/watch?v=NpmiL7zaUvU&list=PLxAS51iVMjv92yR95B2H5ZiR2y1yijglM
• Google drive resource activities:
https://tinyurl.com/MLE-G11IT-DateAndTime

For more IT related material find us on:

youtube.com/user/MrLongEducation

You might also like