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
Replay: real traced execution (multi-file project)
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');
}
  1. d ← DateTime.utc(2024-07-04 09:30:00)

    1void main() {2  var d = DateTime.utc(2024, 7, 4, 9, 30);3  var iso = d.toIso8601String();
    values this stepDateTime.utc(2024-07-04 09:30:00)d
  2. iso ← 2024-07-04T09:30:00.000Z

    2var d = DateTime.utc(2024, 7, 4, 9, 30);3var iso = d.toIso8601String();4var year = d.year;
    values this step2024-07-04T09:30:00.000Ziso
  3. year ← 2024

    3var iso = d.toIso8601String();4var year = d.year;5var month = d.month;
    values this step2024year2024d.year
  4. month ← 7

    4var year = d.year;5var month = d.month;6print('iso=$iso');
    values this step7month7d.month
  5. print('iso=$iso');

    5  var month = d.month;6  print('iso=$iso');7}
    outputiso=2024-07-04T09:30:00.000Z
    values this step2024-07-04T09:30:00.000Ziso
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.