Sub topic 6
Plain Dates and Times Without Time Zones
Many dates and times in real programs have nothing to do with a specific moment on Earth's timeline. A birthday, a store's opening hour, a monthly billing period, or "lunch at 12:30 on March 15" each mean the same thing wherever the reader is. Temporal gives these values their own family of types, called the Plain types. They hold wall-clock fields and no time zone.
This chapter assumes you know modern JavaScript syntax and ES module imports, and that you have Temporal available, either natively or through @js-temporal/polyfill. Parsing options, arithmetic rules, and conversions to exact time each get a later chapter of their own. Here the focus is on what each Plain type represents and when to choose it.
The five Plain types
Each Plain type stores only the fields it needs. If a type has no field for something, you can't set it by accident.
| Type | Fields | String form | Typical use |
|---|---|---|---|
| `PlainDate` | year, month, day | `2024-03-15` | Due dates, holidays, birth dates |
| `PlainTime` | hour through nanosecond | `14:30:00` | Opening hours, alarm times |
| `PlainDateTime` | date fields plus time fields | `2024-03-15T14:30:00` | A local appointment before a zone is known |
| `PlainYearMonth` | year, month | `2024-02` | Billing periods, credit card expiry |
| `PlainMonthDay` | month, day | `02-29` | Recurring anniversaries |
None of these types can tell you which instant they correspond to. "2024-03-15T09:00" happens at a different moment in Tokyo than in Paris. To get a real moment you must add a time zone, which turns the value into a ZonedDateTime.
PlainDate: a calendar date
PlainDate is the type you'll use most. Besides its stored fields it exposes computed properties, so you rarely need helper functions.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const date = Temporal.PlainDate.from('2024-03-15');
console.log(date.year, date.month, date.day); // 2024 3 15
console.log(date.monthCode); // 'M03'
console.log(date.dayOfWeek); // 5 (Monday is 1, Sunday is 7)
console.log(date.dayOfYear); // 75
console.log(date.daysInMonth); // 31
console.log(date.inLeapYear); // trueMonths start at 1, unlike Date.prototype.getMonth(), which starts at 0. dayOfWeek follows ISO 8601, so Sunday is 7, not 0 as with Date.prototype.getDay().
PlainTime: a time of day
PlainTime holds a time on a 24-hour clock, down to nanoseconds. Its arithmetic wraps around midnight because it has no date to carry the overflow into.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const opening = new Temporal.PlainTime(9, 0);
const lateShift = Temporal.PlainTime.from('23:30');
console.log(opening.toString()); // '09:00:00'
console.log(lateShift.add({ hours: 1 }).toString()); // '00:30:00' (wrapped)
console.log(lateShift.hour, lateShift.minute); // 23 30PlainDateTime: date and time, no zone
PlainDateTime combines a date with a time. Use it for a local event whose zone you don't know yet, such as a form field filled in before the user picks a location. You can build one from parts and split it back into them.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const day = Temporal.PlainDate.from('2024-03-15');
const time = Temporal.PlainTime.from('23:30');
const dt = day.toPlainDateTime(time);
console.log(dt.toString()); // '2024-03-15T23:30:00'
console.log(dt.add({ hours: 1 }).toString()); // '2024-03-16T00:30:00'
console.log(dt.toPlainDate().toString()); // '2024-03-15'
console.log(dt.toPlainTime().toString()); // '23:30:00'PlainYearMonth and PlainMonthDay: partial dates
Some requirements have no day or no year. Padding them with a fake value, such as day 1 or year 2000, causes bugs when that fake value leaks into a calculation. The partial types avoid this.
import { Temporal as TemporalPolyfill } from '@js-temporal/polyfill';
const Temporal = globalThis.Temporal ?? TemporalPolyfill;
const period = Temporal.PlainYearMonth.from('2024-02');
console.log(period.daysInMonth); // 29
console.log(period.add({ months: 1 }).toString()); // '2024-03'
const leapBirthday = Temporal.PlainMonthDay.from({ month: 2, day: 29 });
console.log(leapBirthday.toString()); // '02-29'
// Supply the missing year to get a full date
console.log(leapBirthday.toPlainDate({ year: 2024 }).toString()); // '2024-02-29'
console.log(leapBirthday.toPlainDate({ year: 2023 }).toString()); // '2023-02-28'In a non-leap year, February 29 is clamped to February 28. This is the default constrain behavior, which a later chapter covers. Going the other direction, PlainDate has toPlainYearMonth() and toPlainMonthDay(), which drop the fields you don't need.
Common mistakes
- Comparing different types. A
PlainDateand aPlainDateTimeare never equal, even for the same day. Convert one first. - Using
PlainMonthDayarithmetic. It has noaddorsubtract. Without a year, "plus one month" is undefined. Convert to aPlainDatefirst. - Assuming "today" is zone-free. The current date depends on where you are.
Temporal.Now.plainDateISO()reads the system zone, so pass a zone explicitly on servers. - Reaching for
PlainDateTimeby habit. If the value has no time component, aPlainDatekeeps code simpler and removes midnight edge cases.
Exercise
An HR app stores each employee's birthday and must show the date of this year's celebration. A teammate stores birthdays as PlainDate values with the real birth year. Which type fits better, and how do you get the 2025 celebration date for someone born on 2000-02-29?
Show answer
PlainMonthDay fits better, because the celebration repeats every year and the birth year plays no part in it. Store Temporal.PlainMonthDay.from({ month: 2, day: 29 }), then call toPlainDate({ year: 2025 }), which returns '2025-02-28' because the default overflow behavior clamps the invalid day.
Exercise
A night-shift scheduler computes the end time with Temporal.PlainTime.from('22:00').add({ hours: 8 }) and gets '06:00:00', but the shift report shows the wrong day. Why, and what's the fix?
Show answer
PlainTime has no date, so adding 8 hours wraps past midnight and the change of day is lost. Combine the start date and time into a PlainDateTime, for example date.toPlainDateTime(Temporal.PlainTime.from('22:00')).add({ hours: 8 }), which moves the date forward to the next day.