Date Methods:
We Need to know the date methods to write a efficient Queries in SOQL and SOSL.
The following are the instance methods for Date.Name | Arguments | Return Type | Description |
addDays | Integer addlDays | Date |
Adds the specified number of addlDays to a Date. For example:
date myDate =
date.newInstance(1960, 2, 17);
date newDate = mydate.addDays(2);
|
addMonths | Integer addlMonths | Date | Adds the specified number of addlMonths to a Date |
addYears | Integer addlYears | Date | Adds the specified number of addlYears to a Date |
day | Integer | Returns the day-of-month component of a Date. For example, February 5, 1999 would be day 5. | |
dayOfYear | Integer | Returns the day-of-year component of a Date. For example, February 5, 1999 would be day 36. | |
daysBetween | Date compDate | Integer | Returns the number of days between the Date that called the method and the compDate. If the Date that calls the method occurs after the compDate, the return value is negative. For example:
date startDate =
date.newInstance(2008, 1, 1);
date dueDate =
date.newInstance(2008, 1, 30);
integer numberDaysDue =
startDate.daysBetween(dueDate);
|
format | String | Returns the Date as a string using the locale of the context user | |
isSameDay | Date compDate | Boolean |
Returns true if the Date that called the method is the same as the compDate. For example:
date myDate = date.today();
date dueDate =
date.newInstance(2008, 1, 30);
boolean dueNow = myDate.isSameDay(dueDate);
|
month | Integer | Returns the month component of a Date (1=Jan) | |
monthsBetween | Date compDate | Integer | Returns the number of months between the Date that called the method and the compDate, ignoring the difference in dates. For example, March 1 and March 30 of the same year have 0 months between them. |
toStartOfMonth | Date | Returns the first of the month for the Date that called the method. For example, July 14, 1999 returns July 1, 1999. | |
toStartOfWeek | Date |
Returns the start of the week for the Date that called the method, depending on the context user's locale. For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:
date myDate = date.today();
date weekStart = myDate.toStartofWeek();
| |
year | Integer | Returns the year component of a Date |