Date Methods

The following are the system static methods for Date.

Name Arguments Return Type Description
daysInMonth Integer year

Integer month

Integer Returns the number of days in the month for the specified year and month (1=Jan) The following example finds the number of days in the month of February in the year 1960:
Integer numberDays = 
        date.daysInMonth(1960, 2);
isLeapYear Integer year Boolean Returns true if the specified year is a leap year
newInstance Integer year

Integer month

Integer date

Date Constructs a Date from Integer representations of the year, month (1=Jan), and day. The following example creates the date February 17th, 1960:
Date myDate = 
        date.newinstance(1960, 2, 17);
parse String Date Date Constructs a Date from a String. The format of the String depends on the local date format. The following example works in some locales:
date mydate = date.parse('12/27/2009');
today Date Returns the current date in the current user's time zone
valueOf String s Date Returns a Date that contains the value of the specified String. The String should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone. For example:
string year = '2008';
string month = '10';
string day = '5';
string hour = '12';
string minute = '20';
string second = '20';
string stringDate = year + '-' + month
 + '-' + day + ' ' + hour + ':' + 
minute + ':' + second;

Date myDate = date.valueOf(stringDate);
valueOf Object fieldValue Date

Converts the specified history tracking field value to a Date.

Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date field.

Example:

List<AccountHistory> ahlist = 
  [SELECT Field,OldValue,NewValue
   FROM AccountHistory];
for(AccountHistory ah : ahlist) {
  System.debug('Field: ' + ah.Field);
  if (ah.field == 'MyDate__c') {
    Date oldValue = 
      Date.valueOf(ah.OldValue);
    Date newValue = 
      Date.valueOf(ah.NewValue);
}

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

For more information on Dates, see Primitive Data Types.

© Copyright 2000–2013 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.