The DateComponents
class provides a flexible way to represent and manipulate individual components of a date and time value, such as year, month, day, hour, minute, second, and more. This class is modeled after Swift’s DateComponents
and integrates with the current system calendar.
The constructor accepts an optional options
object to initialize date/time fields:
DateComponents.fromDate(date: Date): DateComponents
Creates a DateComponents
instance by extracting all possible components from a given Date
object.
date
(Date
): The source date.DateComponents
instance with year, month, day, hour, minute, second, and nanosecond set.DateComponents.forHourly(date: Date): DateComponents
Creates a DateComponents
instance representing the hourly trigger for scheduling purposes.
minute
DateComponents.forDaily(date: Date): DateComponents
Creates a DateComponents
instance representing the daily trigger.
hour
, minute
DateComponents.forWeekly(date: Date): DateComponents
Creates a DateComponents
instance for weekly triggers, useful for weekly recurring events.
weekday
, hour
, minute
DateComponents.forMonthly(date: Date): DateComponents
Creates a DateComponents
instance representing a monthly trigger.
day
, hour
, minute
date?: Date | null
The computed Date
object based on the current components using the current system calendar. Returns null
if the components do not form a valid date.
isValidDate: boolean
Indicates whether the current combination of components forms a valid date.
Each of these fields can be initialized via the constructor or set manually afterward. All are number | null
unless otherwise stated.
era
: The era of the date.
year
: The year component.
yearForWeekOfYear
: The year that corresponds to the week-based calendar.
quarter
: The quarter of the year (1 to 4).
month
: The month of the year (1 to 12).
isLeapMonth
: Optional boolean indicating whether the month is a leap month.
weekOfMonth
: The week number within the current month.
weekOfYear
: The week number within the current year.
weekday
: The day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday).
weekdayOrdinal
: The ordinal occurrence of the weekday in the month.
day
: The day of the month.
hour
: The hour of the day (0–23).
minute
: The minute (0–59).
second
: The second (0–59).
nanosecond
: The nanosecond part of the time.
dayOfYear
: The day of the year (1–366).
Or create from a Date
:
date
and isValidDate
rely on the system’s calendar.forDaily
, forWeekly
, etc.) for easier creation of recurring triggers.DateComponents
is ideal for scheduling notifications, alarms, calendar events, and more.