DateTime class in Dart represents an immutable, instantaneous point in time, evaluated within the Gregorian calendar system. It encapsulates both date and time components, operates with up to microsecond precision, and natively supports both the local system time zone and Coordinated Universal Time (UTC).
Instantiation
DateTime objects are instantiated using the default constructor for local time, or via several named constructors for specific time contexts and parsing.
Core Properties
Once instantiated, aDateTime object exposes its individual chronological components as integer properties. Because DateTime is immutable, these properties are read-only.
Time Zone Conversion
Dart handles time zones strictly as either Local or UTC. You can project aDateTime instance from one context to the other.
Arithmetic Operations
Dart distinguishes between absolute time arithmetic and calendar time arithmetic. This distinction is critical when dealing with local time, as Daylight Saving Time (DST) transitions can alter the length of a calendar day. Absolute Time Arithmetic Theadd and subtract methods apply a Duration (an absolute number of microseconds) to a DateTime. Because a Duration of 1 day is exactly 24 hours, adding it to a local DateTime across a DST boundary will shift the resulting local wall-clock time. To guarantee deterministic absolute arithmetic, operations should be performed in UTC.
DateTime constructor to overflow the specific date components. Dart automatically normalizes out-of-bounds values.
Comparison Methods
DateTime implements Comparable<DateTime> and provides semantic methods for chronological comparison. Dart does not overload standard relational operators (<, >, <=, >=) for DateTime; attempting to use them results in a compile-time error. Developers must use isBefore, isAfter, or compareTo.
Additionally, the equality operator (==) evaluates both the absolute moment in time and the time zone state (UTC vs. Local). To compare absolute moments regardless of time zone, use isAtSameMomentAs.
Serialization and Parsing
Dart natively supports the ISO-8601 standard for serializingDateTime objects to strings and parsing strings back into DateTime objects.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





