Notes
Date class contains methods for the Date primitive data type.
newInstance(year, month, day):
Constructs a Date from Integer representations of the year, month (1=Jan), and day.
Date myDate = date.newinstance(2022, 1, 17);
System.debug(myDate);
day():
Returns the day-of-month component of a Date.
date myDate = date.newInstance(2022, 2, 3);
Integer day = myDate.day();
system.debug(day);
month();
Returns the month component of a Date (1=Jan).
date myDate = date.newInstance(2004, 11, 21);
System.debug(myDate.month());
year():
date myDate = date.newInstance(1988, 12, 17);
system.debug(myDate.year());
today():
Returns the current date in the current user's time zone.
date myDate = date.today();
System.debug(myDate);
addDays(additionalDays):
Adds the specified number of additional days to a Date.
The type of the parameter is an integer.
Date oldDate = Date.newInstance(2022, 2, 12);
Date updatedDate = oldDate.addDays(2);
System.debug(updatedDate);
addMonths():
Adds the specified number of additional months to a Date.
Date oldDate = Date.newInstance(2022, 2, 12);
Date updatedDate = oldDate.addMonths(2);
System.debug(updatedDate);
addYears():
Adds the specified number of additional years to a Date.
Date oldDate = Date.newInstance(2000, 2, 12);
Date updatedDate = oldDate.addYears(4);
System.debug(updatedDate);
dayOfYear():
date myDate = date.newInstance(2022, 12, 1);
Integer day = myDate.dayOfYear();
system.debug(day);
Note: The first day of December is the 335th day of the year.
daysBetween(secondDate):
Returns the number of days between the Date that called the method and the specified date.
Date startDate = Date.newInstance(2008, 1, 1);
Date dueDate = Date.newInstance(2008, 1, 30);
system.debug(startDate.daysBetween(dueDate));
daysInMonth(year, month):
Returns the number of days in the month for the specified year and month (1=Jan).
Integer numberDays = date.daysInMonth(2022, 5);
System.debug(numberDays);
format():
Returns the Date as a string using the locale of the context user.
isLeapYear(year):
Returns true if the specified year is a leap year.
system.debug(Date.isLeapYear(2004));
isSameDay(dateToCompare):
Returns true if the Date that called the method is the same as the specified date.
date myDate = date.today();
date dueDate = date.newInstance(2022, 2, 3);
System.debug(myDate.isSameDay(dueDate));
monthsBetween(secondDate):
Date firstDate = Date.newInstance(2006, 12, 2);
Date secondDate = Date.newInstance(2012, 12, 8);
System.debug(firstDate.monthsBetween(secondDate));
Returns the number of months between the Date that called the method and the specified date, ignoring the difference in days.
parse(stringDate):
Constructs a Date from a String. The format of the String depends on the local date format.
date mydate = date.parse('12/27/2009');
System.debug(myDate);
toStartOfMonth():
Returns the first of the month for the Date that called the method.
date myDate = date.newInstance(1987, 12, 17);
system.debug(myDate.toStartOfMonth());
toStartOfWeek():
Returns the start of the week for the Date that called the method, depending on the context user's locale.
Date myDate = Date.today();
System.debug(myDate.toStartofWeek());
Note: For example, the start of a week is Sunday in the United States locale, and Monday in European locales. For example:
The day for 2022-01-30 is Monday.