Async and Practical
DateTime
construct in UTC
DateTime.utc(year, month, day, ...) builds a moment in Coordinated Universal Time. Building in UTC keeps the example independent of the host time zone, and toIso8601String() formats it as a stable text representation.
Program
Play the program to build a UTC DateTime, read two fields, and print the ISO string.
datetime_construct.dart
void main() {
var d = DateTime.utc(2024, 7, 4, 9, 30);
var iso = d.toIso8601String();
var year = d.year;
var month = d.month;
print('iso=$iso');
}
DateTime.utc
`DateTime.utc(year, month, day, hour, minute)` builds a UTC moment; field defaults fill in the rest.
field access
`d.year`, `d.month`, and the rest expose calendar components as integers.
toIso8601String
Formats the moment as a stable ISO-8601 string; the trailing `Z` marks UTC.