Sub topic 6
Date, Date/Time and Time
This chapter assumes you can create a custom field in Object Manager and know what a record's API name is. It covers the three field types that store points in time and how each one handles time zones.
The three types
| Type | Stores | Time zone behavior | Apex type |
|---|---|---|---|
| Date | Calendar day only | None. The same day for every user. | `Date` |
| Date/Time | Day and time | Stored in UTC, shown in each user's time zone | `Datetime` |
| Time | Time of day only | No date and no time zone in Apex | `Time` |
Choose by asking what the value means. A birthday or contract end date is the same day everywhere, so use Date. A meeting start or a case closing moment is a single instant, so use Date/Time. A store opening hour is a clock time that repeats daily, so use Time.
Time zones: the common source of bugs
A Date/Time value is stored in UTC. The UI converts it to the viewing user's time zone. Two users in different zones see different clock times for the same record, and that is correct.
Problems start when you convert between Date/Time and Date. Take a Date/Time of 23:30 UTC on 30 November. A user in Sydney sees 1 December. Converting that value to a Date gives a different day depending on whose time zone is used.
Datetime dt = Datetime.newInstanceGmt(2026, 11, 30, 23, 30, 0);
System.debug(dt.dateGmt()); // 2026-11-30 (UTC day)
System.debug(dt.date()); // day in the running user's time zone
Date d = Date.newInstance(2026, 11, 30);
System.debug(d.addDays(7)); // 2026-12-07
Time t = Time.newInstance(9, 0, 0, 0);
System.debug(t.addHours(2)); // 11:00:00.000ZUse the Gmt variants when you need a predictable result that does not depend on the running user.
Dates in formulas and SOQL
In formulas, TODAY() returns a Date and NOW() returns a Date/Time. Subtracting one Date from another returns the number of days as a number. Adding a number to a Date/Time adds days, so NOW() + 0.5 is twelve hours later. DATEVALUE() converts a Date/Time to a Date using the user's time zone.
In SOQL, date literals are written without quotes. Date/Time literals use the ISO format and a Z suffix for UTC.
List<Opportunity> recent = [
SELECT Id, Name, CloseDate
FROM Opportunity
WHERE CloseDate = LAST_N_DAYS:30
];
List<Case> opened = [
SELECT Id FROM Case
WHERE CreatedDate >= 2026-11-01T00:00:00Z
];Limits to remember
- Changing a Date field to Date/Time (or the reverse) is allowed in some cases, but the conversion can shift days. Check the existing data first.
- Time fields cannot be used in roll-up summary fields. Date and Date/Time fields can, for MIN and MAX.
- Standard Date/Time fields such as
CreatedDateare read-only and set by the system.
Exercise
A "Warranty Expires" value must show the same day to a user in London and a user in Tokyo. Which field type should you use, and why?
Show answer
Date. It stores only a calendar day and is not converted by time zone. A Date/Time field would be stored in UTC and could display as different days in London and Tokyo.