TimePeriod
Represents a time period composed of several date and time fields. Can be used to add or subtract an amount from a CalendarDate, or as the result of CalendarDate.difference.
Syntax
object { year, month, week, day, hour, minute, second, millis }
Properties
year: number
- Positive or negative years
month: number
- Positive or negative months
week: number
- Positive or negative weeks
day: number
- Positive or negative days
hour: number
- Positive or negative hours
minute: number
- Positive or negative minutes
second: number
- Positive or negative seconds
millis: number
- Positive or negative milliseconds
Example
const cldr = framework.get('en');
const zoneId = 'America/New_York';
const opts: DateFormatOptions = { datetime: 'long' };
const deltas: TimePeriod[] = [
{ year: 1, month: -2 },
{ year: 5, day: 17, hour: -5 },
{ year: -10, month: 2 },
];
const base = { date: new Date(2018, 6, 1, 14, 15, 16), zoneId };
const date = cldr.Calendars.toGregorianDate(base);
log(cldr.Calendars.formatDate(date, opts));
for (const delta of deltas) {
const result = cldr.Calendars.formatDate(date.add(delta), opts);
log();
log(delta);
log(result);
}
July 1, 2018 at 2:15:16 PM EDT { year: 1, month: -2 } May 1, 2019 at 2:15:16 PM EDT { year: 5, day: 17, hour: -5 } July 18, 2023 at 9:15:16 AM EDT { year: -10, month: 2 } September 1, 2008 at 2:15:16 PM EDT