There are basically two operators for date, datetime values. + and - are overloaded (probably a C term) to do date/datetime math:
Operator | Description |
---|---|
+ | Adds days (date) or seconds (datetime) to a date/datetime value. |
- | Gets the difference of two date/datetime values. Subtracts days (date) or seconds (datetime) from datetime values. |
+ is the easier one. It has two operands, one is a date or datetime value and the other is a numeric (although you might use any numeric, it is an integer for all practical purposes).
When one of the operands is a date, then numeric operand is taken as "day":
? Date() + 10 && Get the date 10 days later
* VFP is leap year aware when doing date math
? Date(2016, 2, 28) + 1 && Add 1 day to Feb 28, 2016. Returns Feb 29, 2016.
? Date(2017, 2, 28) + 1 && Add 1 day to Feb 28, 2017. Returns Mar 1, 2017.
When one of the operands is a datetime, then numeric operand is taken as "second":
There are 24 * 60 * 60 = 86400 seconds in a day
? Datetime() + 86400 && Add 1 day to current datetime.
Add 4 hours and 15 minutes to Jan 1, 2016 2:20 PM
? Datetime(2016, 1, 1, 14, 20, 0) + (4 * 3600 + 15 * 60)
Outputs Friday, January 1, 2016, 6:35:00 PM.
With a simple printing using ?, what you see on your screen is dependant on your date settings. For example, if you haven't changed anything your date settings are American style (MDY), 12 hours format (AM/PM) and century is shown with last 2 digits only.
There is one special symbol ^ for date and datetimes forcing a string to be interpreted 'strictly' as yyyy/MM/dd [HH:mm:ss|hh:mm:ss tt] format. Therefore ^ might be considered as date/datetime operator as well. For example consider some data is incoming from a source in a format like 201610082230 (yyyyMMddHHmm). To get that value as a valid Datetime:
Local cSample, tSample
cSample = '201610082230'
tSample = Ctot(Transform(m.cSample, '@R ^9999/99/99 99:99'))
? Transform(m.tSample, '@YL')
Outputs (depending on your system's long date setting):
Saturday, October 8, 2016, 10:30:00 PM
- is used for subtraction. Its operands are either both are date/datetime values OR one is a date/datetime and the other is a numeric.
Let's start with the simpler date/datetime and numeric operands (like with + operator):
When one of the operands is a date, then numeric operand is taken as "day":
? Date() - 10 && What was the date 10 days ago?
? Date(2016, 3, 1) - 1 && Returns Feb 29, 2016.
? Date(2017, 3, 1) - 1 && Returns Feb 28, 2017.
When one of the operands is a datetime, then numeric operand is taken as "second":
? Datetime() - 86400 && Go back exactly one day
Get 1 hour and 30 minutes ago from "now":
? Datetime() - (1 * 3600 + 30 * 60)
Second form is to get the difference between two date/datetime values. Operands are both date or datetime, you cannot use date and datetime at the same time (do type conversion as needed, VFP doesn't do that for you). Rules are as in + and -, operands are date then the difference is in days, operands are datetime then the difference is in seconds.
How many days to new year's eve (for year 2016)?
? Date(2016, 12, 31) - Date()
How many seconds left to midnight?
? Dtot(Date()+1) - Datetime()
In the last sample we used a Date/Datetime function, DTOT - DateToTime for getting tomorrow's midnight value. There are many useful date/datetime functions available, we skipped them all as they are technically not considered operators (although they operate on date/datetimes:) Same goes true with other operators as well.
The date/datetime subtraction is signed. That is if you use the smaller date/datetime as the first operand then the result would be negative. You might use the abs() function if you need to get a positive result regardless of the order of date/datetimes.