3

Date Datatypes 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: 

Date Methods in Apex
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.

Book A 15-Minutes Free Career Counselling Today!