3

Date Datatypes in APEX

What is the Date Data Type in Salesforce?

A variable of the Date DataType in Apex stores a date value. Date values contain no information about time. Dates in Apex are represented by the Date data type, which includes the day, month, and year information. This works great when you have to work with dates, perform computations, or compare dates without accounting for time. It is always advised to create date values with a system static method.

We can add or subtract an Integer value from a Date value, returning a Date value. Addition and subtraction of Integer values are the only arithmetic functions that work with Date values. However, we can’t perform arithmetic functions that include two or more Date values.

How to write a date data type in Salesforce?

Declaration and Initialization // Declaration Date myDate; // Initialization myDate = Date.newInstance(2024, 5, 16); // Specific date Date today = Date.today(); // Current date Date parsedDate = Date.valueOf('2024-05-16'); // Parsing a date from string

Date Methods in Apex

There are various Apex date methods in Salesforce to use the Date DataType in Apex: 

S.No Function Example
1 addDays(additionalDays) Adds the specified number of additional days to a Date Date today = Date.today(); Date futureDate = today.addDays(10); System.debug('10 Days from Today: ' + futureDate);
2 addMonths(additionalMonths) Adds the specified number of additional months to a Date Date today = Date.today(); Date futureDate = today.addMonths(2); System.debug('2 Months from Today: ' + futureDate);
3 addYears(additionalYears) Adds the specified number of additional years to a Date Date today = Date.today(); Date futureDate = today.addYears(1); System.debug('1 Year from Today: ' + futureDate);
4 day() Returns the day-of-month component of a Date. Date today = Date.today(); Integer dayOfMonth = today.day(); System.debug('Day of Month: ' + dayOfMonth);
5 dayOfYear() Returns the day-of-year component of a Date. Date today = Date.today(); Integer dayOfYear = today.dayOfYear(); System.debug('Day of Year: ' + dayOfYear);
6 daysBetween(secondDate) Returns the number of days between the Date that called the method and the specified date. Date startDate = Date.newInstance(2024, 5, 1); Date endDate = Date.newInstance(2024, 5, 16); Integer daysDiff = startDate.daysBetween(endDate); System.debug('Days between: ' + daysDiff);
7 daysInMonth(year, month) Returns the number of days in the month for the specified year and month (1=Jan). Integer daysInMonth = Date.daysInMonth(2024, 5); // May 2024 System.debug('Days in May 2024: ' + daysInMonth);
8 format() Returns the Date as a string using the locale of the context user Date today = Date.today(); String formattedDate = today.format(); System.debug('Formatted Date: ' + formattedDate);
9 isLeapYear(year) Returns true if the specified year is a leap year. Boolean isLeap = Date.isLeapYear(2024); System.debug('Is 2024 a leap year? ' + isLeap);
10 isSameDay(dateToCompare) Returns true if the Date that called the method is the same as the specified date. Date today = Date.today(); Date sameDate = Date.today(); Boolean isSame = today.isSameDay(sameDate); System.debug('Is same day? ' + isSame);
11 month() Returns the month component of a Date (1=Jan) Date today = Date.today(); Integer month = today.month(); System.debug('Month: ' + month);
12 monthsBetween(secondDate) Returns the number of months between the Date that called the method and the specified date, ignoring the difference in days. Date startDate = Date.newInstance(2024, 1, 1); Date endDate = Date.newInstance(2024, 5, 1); Integer monthsDiff = startDate.monthsBetween(endDate); System.debug('Months between: ' + monthsDiff);
13 newInstance(year, month, day) Constructs a Date from Integer representations of the year, month (1=Jan), and day. Date specificDate = Date.newInstance(2024, 5, 16); System.debug('Specific Date: ' + specificDate);
14 parse(stringDate) Constructs a Date from a String. The format of the String depends on the local date format. Date parsedDate = Date.parse('2024-05-16'); // Comment by Sanjay Sharma: change it with date.parse('05/16/2024') (month/day/year) System.debug('Parsed Date: ' + parsedDate);
15 today() Returns the current date in the current user's time zone. Date today = Date.today(); System.debug('Today: ' + today);
16 toStartOfMonth() Returns the first of the month for the Date that called the method. Date today = Date.today(); Date startOfMonth = today.toStartOfMonth(); System.debug('Start of Month: ' + startOfMonth);
17 toStartOfWeek() Returns the start of the week for the Date that called the method, depending on the context user's locale. Date today = Date.today(); Date startOfWeek = today.toStartOfWeek(); System.debug('Start of Week: ' + startOfWeek);
18 valueOf(stringDate) Returns a Date that contains the value of the specified String. Date valueOfDate = Date.valueOf('2024-05-16'); System.debug('Value of Date: ' + valueOfDate); // Comment by Sanjay Sharma: you can put sr no 14,18 and 19 together because their work is similar
19 valueOf(fieldValue) Converts the specified object to a Date. Use this method to convert a history tracking field value or an object that represents a Date value. String fieldValue = '2024-05-16'; Date valueOfField = Date.valueOf(fieldValue); System.debug('Value of Field: ' + valueOfField);
20 year() Returns the year component of a Date Date today = Date.today(); Integer year = today.year(); System.debug('Year: ' + year);
salesforce-marketing-cloud-cta
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Frequently Asked Questions

How do I create a Date in Apex Salesforce?

To create a dat ein Slaesforce APex, tehr eare multiple ways. One of the most reliable ones is, Using newInstance: Date specificDate = Date.newInstance(2024, 5, 16);

What is the difference between Date and DateTime in Apex?

Date in Apex stores the date, year, and time. The DateTime in the Apex component holds both date components and time, including hours, minutes, seconds, and milliseconds.

How do I get today's date in Apex?

    To get today’s date in Apex:
  • Date today = Date.today();
  • // Returns the current date in the context user's time zone

How do I convert a string to a Date in Apex Salesforce?

    To convert a string to a date, the best way is to use “Date.ValueOf()” to get the string in yyyy-mm-dd format. Example:
  • Date d = Date.valueOf('2024-05-16');

Book Free15-Minutes Career Counselling