Date Manipulation
Getting the Current Date
There is a special function to return the current date (with no time) and the current date and time. These are CurrentDate and CurrentDateTime. These will be demonstrated later on.
Formatting a Date
Go to Language Tab > Functions > DateTime:
FormatDateTime(format, DateTime);
Format can be any string, using any combination of these keywords (for a complete list, see ). The example is based on the date and time “6 January 2008 1:23:45pm”:
Format |
Description |
Example |
d |
single-digit date |
6 |
dd |
double-digit date |
06 |
ddd |
short day name |
Sun |
dddd |
long day name |
Sunday |
m |
single-digit month |
1 |
mm |
double-digit month |
01 |
mmmm |
full month name |
January |
yy |
two-digit year |
08 |
yyyy |
four digit year |
2008 |
hh |
hours |
01 |
MM |
minutes |
23 |
ss |
seconds |
45 |
am/pm |
am or pm |
pm |
Example 1:
This formats the date as in: 'Monday, 19 April 2008':
Text := FormatDateTime('dddd, dd mmmm yyyy',Dr_Accs['Startdate']);
Example 2:
This function can also be used to get part of a date:
Text := FormatDateTime('mm', CurrentDate);
This would return the month part of the current date only (e.g. it would show '04' for April).
Manipulating Dates
There are functions that allow you to manipulate dates, doing things such as adding or subtracting a number of days, months or years. These follow all the rules regular calendars, including taking into account leap-years.
Days
To add or subtract days to or from a date, simply use the + and – arithmetic operators. For example, this adds 30 days to the current date:
Variable1.Value := CurrentDate + 30;
Note that similarly simple algebra can also be used to find out the number of days between two dates by subtracting one from the other. Without going into too much detail, all dates are stored internally as a number of days since 30 December 1899, so you can use this knowledge to your advantage in complex date calculations (The reasons for this date in particular are well beyond the scope of this document).
Months
To add or subtract months to or from a date, you need to use the IncMonth( ) function. This takes into account the number of days per month including February 29th in leap years when applicable. For example, this subtracts 3 months from the current date:
Variable1.Value := IncMonth(CurrentDate, -3);
Years
To add or subtract years to or from a date, it's easiest to just use IncMonth( ) with a multiple of 12 months. This takes into account leap years when applicable. For example, this adds 3 years to the current date:
Variable1.Value := IncMonth(CurrentDate, 3 * 12);
Other Date Functions
Clarity also has other more advanced Date functions for creating or breaking down Date and DateTime values (EncodeDate / EncodeTime and DecodeDate / DecodeTime) as well as finding out how many weeks, months or years between two dates (WeeksBetween, MonthsBetween and YearsBetween), and even finding out what day of the week a given date falls on (DayOfWeek). This kind of advanced date manipulation is outside the scope of this document but they are standard functions in Delphi and any good Delphi tutorial will cover the topics in detail.