Experience Salesforce

Date Datatypes in APEX

What You’ll Learn

  • What is the Date Data Type in Salesforce?
  • Date Methods in Apex

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.NoFunctionExample
1addDays(additionalDays)Adds the specified number of additional days to a DateDate today = Date.today();Date futureDate = today.addDays(10);System.debug(’10 Days from Today: ‘ + futureDate);
2addMonths(additionalMonths)Adds the specified number of additional months to a DateDate today = Date.today();Date futureDate = today.addMonths(2);System.debug(‘2 Months from Today: ‘ + futureDate);
3addYears(additionalYears)Adds the specified number of additional years to a DateDate today = Date.today();Date futureDate = today.addYears(1);System.debug(‘1 Year from Today: ‘ + futureDate);
4day()Returns the day-of-month component of a Date.Date today = Date.today();Integer dayOfMonth = today.day();System.debug(‘Day of Month: ‘ + dayOfMonth);
5dayOfYear()Returns the day-of-year component of a Date.Date today = Date.today();Integer dayOfYear = today.dayOfYear();System.debug(‘Day of Year: ‘ + dayOfYear);
6daysBetween(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);
7daysInMonth(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 2024System.debug(‘Days in May 2024: ‘ + daysInMonth);
8format()Returns the Date as a string using the locale of the context userDate today = Date.today();String formattedDate = today.format();System.debug(‘Formatted Date: ‘ + formattedDate);
9isLeapYear(year)Returns true if the specified year is a leap year.Boolean isLeap = Date.isLeapYear(2024);System.debug(‘Is 2024 a leap year? ‘ + isLeap);
10isSameDay(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);
11month()Returns the month component of a Date (1=Jan)Date today = Date.today();Integer month = today.month();System.debug(‘Month: ‘ + month);
12monthsBetween(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);
13newInstance(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);
14parse(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’);System.debug(‘Parsed Date: ‘ + parsedDate);
15today()Returns the current date in the current user’s time zone.Date today = Date.today();System.debug(‘Today: ‘ + today);
16toStartOfMonth()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);
17toStartOfWeek()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);
18valueOf(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);
19valueOf(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);
20year()Returns the year component of a DateDate today = Date.today();Integer year = today.year();System.debug(‘Year: ‘ + year);
salesforce-marketing-cloud-cta

Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img