Date extension type
An immutable type representing a calendar date without time components. All dates are stored internally in UTC timezone. Any non-UTC input DateTime will be converted to UTC during construction.
Note the behavior of extension types in Dart (https://dart.dev/language/extension-types). For most intends and purposes a Date is a DateTime but the time component is not accessible. All operations are performed in UTC. See the GitHub repo for a discussion on whether this type should be a class or an extension type.
- on
Constructors
- Date.new(DateTime dateTime)
- Creates a Date from a DateTime, stripping all time components and converting to UTC. If the input DateTime is not in UTC, it will be converted automatically.
- Date.fromValues({required int year, required int month, required int day})
- Creates a Date from year, month, and day values. The resulting Date will be in UTC.
- Date.today()
-
Creates a Date representing the current date in UTC, using Zone-provided
time if available.
factory
Properties
- asDateTime → DateTime
-
Returns the underlying UTC DateTime. The time component will always be
00:00:00 UTC.
no setter
- day → int
-
The day component of the date (1-31) in UTC
no setter
- month → int
-
The month component of the date (1-12) in UTC
no setter
- weekday → int
-
The day of the week (1-7, where 1 is Monday) in UTC
no setter
- year → int
-
The year component of the date in UTC
no setter
Methods
-
add(
Duration duration) → Date - Returns a new Date by adding the specified duration. The operation is performed in UTC.
-
subtract(
Duration duration) → Date - Returns a new Date by subtracting the specified duration. The operation is performed in UTC.
-
toIso8601String(
) → String - Converts this Date to an ISO 8601 DateTime string. The output will always represent UTC, and include the time component (00:00:00). This is because the Date type is a wrapper around a DateTime, and the DateTime type includes the time component.
-
toLocalDateTime(
) → DateTime - Returns the underlying UTC DateTime converted to local timezone. The time component will always be 00:00:00 in the local timezone. This is primarily used for display purposes.