Sub topic 6
Date, Date/Time and Time
This chapter assumes you can create custom fields in Object Manager and know what a formula field is. It also assumes you have seen SOQL and Apex. It covers the three temporal field types, how each stores data, and the time zone problems that come from choosing the wrong one.
The three types
Salesforce offers three custom field types for temporal data.
| Type | Stores | Time zone behavior | Apex type | Typical use |
|---|---|---|---|---|
| Date | A calendar day | None. The value is the same for every user. | `Date` | Birthdays, due dates, contract end dates |
| Date/Time | A day and a moment in it | Stored in UTC and shown in each user's time zone | `Datetime` | Event starts, audit stamps, SLA deadlines |
| Time | A time of day with no date | No date, so no calendar offset | `Time` | Opening hours, recurring daily windows |
The standard fields CloseDate on Opportunity and ActivityDate on Task are Date fields. CreatedDate and LastModifiedDate are Date/Time fields.
Date: a day with no time zone
A Date field holds a year, month and day. Two users in different time zones see the same value. This is the right choice when the business meaning is "this calendar day", such as an invoice due date or a renewal date.
Do not store a Date as a Date/Time at midnight. A midnight value in UTC displays as the previous evening for users west of Greenwich, so records appear to have the wrong day.
Date/Time: one moment, many displays
A Date/Time field stores a single instant in UTC. The UI converts it to the viewing user's time zone. Two users see different clock times for the same instant. Use it when the exact moment matters, such as when a meeting starts or a case was escalated.
The conversion causes most date bugs. A record created at 23:30 UTC on 31 December shows as 1 January for a user in UTC+2. Reports that group by day use the viewer's time zone, so daily counts can differ between users.
Time: a time of day only
A Time field holds hours, minutes, seconds and milliseconds. It has no date, so it suits recurring values like "store opens at 09:00". You cannot do calendar arithmetic with it directly. Combine it with a Date in Apex or a formula when you need a full moment.
Test Time fields in your own org before relying on them across time zones. Check how they display for users with different time zone settings, and how your integrations send and receive them.
Defaults and formulas
A default value for a Date field can be a formula such as TODAY(). For a Date/Time field, use NOW(). Mixing them up makes a field default to the wrong shape of data.
Formulas treat the types differently:
TODAY()returns a Date.NOW()returns a Date/Time.- Subtracting two Dates gives a number of days.
- Subtracting two Date/Times gives a decimal number of days.
DATEVALUE()converts a Date/Time to a Date, using the running user's time zone.DATETIMEVALUE()goes the other way.
TODAY() - DATEVALUE(CreatedDate)This formula returns the number of whole days since a record was created. Set its return type to Number with zero decimal places.
(NOW() - CreatedDate) * 24This formula returns the hours since creation, with decimals. A Date/Time difference is in days, so you multiply by 24.
ADDMONTHS(CloseDate, 1) adds a month and handles short months. Adding 30 days with CloseDate + 30 does not keep the same day of the month. Choose the one that matches the business rule.
Formulas have no built-in business-day calendar. If you need working days, you need a longer formula, a Flow, or Apex.
Roll-up summary fields
On a master-detail parent, a roll-up summary can compute MIN or MAX of a child Date or Date/Time field. Use MAX of ActivityDate to show the latest dated child record, or MIN of a start date for the earliest. You cannot sum or average dates.
Dates in Apex
Apex keeps the three types separate, and conversions between them depend on time zone.
Date dueDate = Date.newInstance(2026, 12, 31);
Datetime startsAt = Datetime.newInstanceGmt(2026, 12, 31, 23, 30, 0);
System.debug(startsAt.dateGmt()); // 2026-12-31 00:00:00 (GMT date)
System.debug(startsAt.date()); // date in the running user's time zone
Time opens = Time.newInstance(9, 0, 0, 0);
System.debug(opens.addHours(8)); // 17:00:00.000Z
System.debug(dueDate.addDays(30));startsAt.date() returns 1 January 2027 when the running user is in UTC+2, and 31 December 2026 when the user is in UTC. If your logic must not depend on who runs it, use dateGmt() or compare Datetime values directly.
Dates in SOQL
SOQL writes Date literals as YYYY-MM-DD and Date/Time literals as YYYY-MM-DDThh:mm:ssZ. It also provides date literals such as TODAY, LAST_N_DAYS:n and NEXT_N_DAYS:n.
SELECT Id, Name, CloseDate
FROM Opportunity
WHERE CloseDate >= 2026-11-01 AND CloseDate <= 2026-11-30SELECT Id, Name
FROM Account
WHERE CreatedDate = LAST_N_DAYS:30The literals evaluate in the running user's time zone. A query that runs in a scheduled job as one user can return different rows than the same query run interactively by a user in another zone.
Common mistakes
- Using Date/Time for a plain date. Users in other zones see shifted days. Use Date.
- Using Date for a deadline with a time. You lose the hour. Use Date/Time.
- Comparing a Date to a Date/Time without conversion. The implicit conversion uses a time zone you may not expect. Convert on purpose with
DATEVALUE(). - Splitting a moment into a Date and a Time field. You must then manage time zones yourself, and there is no single field to filter or sort by. Prefer one Date/Time field.
- Importing Date/Time values without a zone. Data Loader interprets them in the zone configured for the loading user, so specify UTC explicitly with a trailing
Z.
Choosing between them
- If the value is a day that means the same thing everywhere, use Date.
- If it is a specific instant that people in different zones coordinate around, use Date/Time.
- If it is a clock time that repeats every day, use Time.
- If you need only the time elapsed between two events, store both as Date/Time and calculate the difference in a formula.
Exercise
A support team wants a field showing when a case must be answered by, for example 14:00 on a given day. Agents work in several time zones. Which type should you use, and how would you show the hours remaining in a formula?
Show answer
Use a Date/Time field, because the deadline is a specific instant that every agent must see in their own local time. For hours remaining, use a Number formula such as (ResponseDue_c - NOW()) * 24, because subtracting Date/Times returns days and multiplying by 24 converts to hours. A Date field would lose the hour, and a Time field would lose the day.