DateTime
Learning Content
Last updated: July 28, 2026
DateTime Data Type
The DateTime data type is used to store both the calendar date and the exact time. It records the year, month, day, hour, minute, and second, making it ideal for applications where the precise moment of an event is important.
Salesforce automatically uses the DateTime data type for system fields such as CreatedDate, LastModifiedDate, and SystemModstamp. It is also widely used for meeting schedules, reminders, asynchronous jobs, integrations, and audit tracking.
Unlike the Date data type, DateTime includes time information and works with Salesforce's time zone handling to display values appropriately for each user.
💡 LearnFrenzy Insight
Imagine scheduling an online interview.
Knowing only the date is not enough.
You also need the exact time, for example:
- 27 July 2026 – 10:30 AM
- 15 August 2026 – 2:00 PM
- 31 December 2026 – 11:59 PM
Whenever both the date and the exact time are required, use the DateTime data type.
What is DateTime?
A DateTime stores both the calendar date and the time of day. It is commonly used to track events, schedule jobs, record timestamps, and measure when business activities occur.
DateTime currentTime = DateTime.now(); System.debug(currentTime);
Why Use DateTime?
| Reason | Benefit |
|---|---|
| Store Date & Time | Captures the exact moment an event occurs. |
| Scheduling | Ideal for meetings, reminders, and scheduled jobs. |
| Audit Tracking | Tracks when records are created or modified. |
| Time Zone Support | Displays values based on the user's time zone. |
Creating DateTime Values
Current Date and Time
DateTime nowTime = DateTime.now(); System.debug(nowTime);
Create a Specific DateTime
DateTime meetingTime = DateTime.newInstance(2026, 7, 27, 10, 30, 0); System.debug(meetingTime);
Adding Time
DateTime startTime = DateTime.now(); DateTime afterFiveDays = startTime.addDays(5); DateTime afterTwoHours = startTime.addHours(2); System.debug(afterFiveDays); System.debug(afterTwoHours);
Useful DateTime Methods
| Method | Description | Example |
|---|---|---|
| DateTime.now() | Returns the current date and time. | DateTime.now() |
| DateTime.newInstance() | Creates a specific DateTime. | DateTime.newInstance(...) |
| addDays() | Adds or subtracts days. | dt.addDays(7) |
| addHours() | Adds or subtracts hours. | dt.addHours(3) |
| addMinutes() | Adds or subtracts minutes. | dt.addMinutes(30) |
| date() | Returns the Date portion. | dt.date() |
| time() | Returns the Time portion. | dt.time() |
Real Salesforce Business Example
🏢 Business Scenario A support team wants to schedule a follow-up call exactly two hours after a Case is created.
DateTime caseCreated = DateTime.now(); DateTime followUpTime = caseCreated.addHours(2); System.debug(followUpTime);
This DateTime value can be used by Flows, Apex, or scheduled automation to trigger reminders at the correct time.
Common Salesforce Use Cases
| Business Requirement | Example |
|---|---|
| CreatedDate | Record creation timestamp |
| LastModifiedDate | Last update timestamp |
| Meeting Schedule | Date and time of a meeting |
| Reminder Notifications | Send alerts at a specific time |
| Scheduled Apex | Execute jobs at defined times |
| Integration Logs | Track API request timestamps |
Date vs DateTime
| Date | DateTime |
|---|---|
| Stores only the calendar date. | Stores both date and time. |
| No time information. | Includes hours, minutes, and seconds. |
| Suitable for birthdays and due dates. | Suitable for meetings and timestamps. |
| No time zone handling. | Displayed according to the user's time zone. |
Time Zone Considerations
📌 Important Note
Salesforce stores DateTime values internally in UTC (Coordinated Universal Time) and automatically converts them to the logged-in user's time zone when displayed.
For example, two users in different countries may see different local times for the same stored DateTime value.
How Salesforce Stores DateTime Values
Developer Creates DateTime
│
▼
Compiler Validates
Date
Time
│
▼
Stored Internally (UTC)
│
▼
Converted to User's Time Zone
│
▼
Displayed in the UI
🔍 Behind the Scenes
User Creates Record
│
▼
CreatedDate Generated
│
▼
Stored in UTC
│
▼
Converted for User
│
▼
Displayed on Record Page
- Use
DateTimewhenever both the date and time are important. - Use built-in methods such as
addDays(),addHours(), andaddMinutes()instead of manual calculations. - Be aware that Salesforce stores DateTime values in UTC and displays them in the user's time zone.
- Use meaningful variable names such as
meetingTime,followUpTime, orcreatedOn.
⚠️ Common Mistakes
- Using
Datewhen the exact time is required. - Ignoring time zone conversions during integrations.
- Writing custom date calculations instead of using built-in DateTime methods.
- Assuming every user sees the same local time for a stored DateTime value.
🎯 Interview Tip
Question: How does Salesforce store DateTime values?
Answer: Salesforce stores DateTime values internally in UTC and automatically converts them to the logged-in user's time zone when displaying them. This ensures consistent storage while presenting the correct local time to each user.
- ✔ Stores both the date and the exact time.
- ✔ Create values using
DateTime.now()orDateTime.newInstance(). - ✔ Supports methods such as
addDays(),addHours(),addMinutes(),date(), andtime(). - ✔ Used for CreatedDate, LastModifiedDate, meetings, reminders, and scheduled jobs.
- ✔ Stored internally in UTC and displayed in the user's time zone.
- ✔ Use Date when only the calendar date is required.
➡ Next Lesson
In the next lesson, you'll explore the Time data type, learn how Apex stores time without a date, and discover real-world scenarios such as business hours, shift schedules, appointment slots, and recurring daily events.
Practice What You've Learned
Test your understanding with these practice exercises