Sub topic 7
Dates, times, and datetimes
This chapter assumes you can declare variables and call methods on them. It covers the three Apex types for time values and the errors people hit most often with them.
Three types, three jobs
| Type | Holds | Example use |
|---|---|---|
| `Date` | Year, month, day | Due date, birthday |
| `Time` | Hour, minute, second, millisecond | Opening hours |
| `Datetime` | A date and a time together | Meeting start, record created timestamp |
Date and Time have no time zone. Datetime is stored as a point in time in GMT. Apex converts it to the running user's time zone when you read or format it.
Creating and shifting dates
Build a Date with Date.newInstance(year, month, day). Date methods return new values and never change the original.
Date start = Date.newInstance(2026, 11, 30);
Date due = start.addDays(14);
Integer gap = start.daysBetween(due);
System.debug(due);
System.debug(gap);
Date endOfJan = Date.newInstance(2026, 1, 31);
System.debug(endOfJan.addMonths(1));The output is 2026-12-14, then 14, then 2026-02-28. addMonths cannot produce February 31, so it moves to the last valid day of that month.
Date.today() returns today's date. Datetime.now() returns the current moment.
Working with Datetime
Two factory methods differ in how they treat the time zone:
Datetime.newInstance(...)reads its arguments as the running user's local time.Datetime.newInstanceGmt(...)reads them as GMT.
Datetime meeting = Datetime.newInstanceGmt(2026, 11, 30, 9, 30, 0);
System.debug(meeting.hourGmt());
System.debug(meeting.dateGmt());
Time opening = Time.newInstance(9, 30, 0, 0);
Datetime local = Datetime.newInstance(Date.newInstance(2026, 11, 30), opening);
System.debug(local.format('yyyy-MM-dd HH:mm'));
System.debug(meeting.formatGmt('yyyy-MM-dd HH:mm'));hourGmt() prints 9 for any user. The local.format line always prints 2026-11-30 09:30, because that value was built in the user's time zone and is formatted back in it. The last line prints 2026-11-30 09:30, because formatGmt ignores the user's zone.
Common errors
- Mixing local and GMT methods.
meeting.hour()andmeeting.hourGmt()give different results for a user outside GMT. Pick one convention for a piece of code. - Assuming a Date has a time zone. It does not. Converting a
Datetimewithdate()uses the user's zone, anddateGmt()uses GMT. Near midnight these can return different days. - Parsing strings with
Date.parse. It depends on the user's locale. UseDate.valueOf('2026-11-30')for the ISO formatyyyy-MM-dd. - Comparing a Date to a Datetime. Convert one to match the other first, for example with
myDatetime.date().
Exercise
A user in a time zone far from GMT builds Datetime.newInstance(Date.newInstance(2026, 11, 30), Time.newInstance(23, 0, 0, 0)). Which methods return the same date and hour for every user, date() and hour() or dateGmt() and hourGmt()? Why?
Show answer
Neither pair is the same for every user. This value was built in the creating user's local time, so date() and hour() return 2026-11-30 and 23 for that user. For a user in another zone they return different values. dateGmt() and hourGmt() return the GMT date and hour, which are the same for everyone but differ from 23 unless the creating user is on GMT. Use the GMT methods when you need a result that does not depend on the user.