Date Datatypes in APEX
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
What is the Date Data Type in Salesforce?
A variable of the Date Data Type in Apex is used to store a date that indicates a particular date. 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 in situations when you have to work with dates, do computations, or compare dates without taking time into account. 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 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 methods to use the Date Data Type 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); |

Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
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.