Dart DateTime.utc()
Syntax & Examples


Syntax of DateTime.utc

The syntax of DateTime.DateTime.utc constructor is:

DateTime.utc(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0])

This DateTime.utc constructor of DateTime constructs a DateTime instance specified in the UTC time zone.

Parameters

ParameterOptional/RequiredDescription
yearrequiredthe year
monthoptional [default value is 1]the month (1 - 12)
dayoptional [default value is 1]the day of the month (1 - 31)
houroptional [default value is 0]the hour of the day (0 - 23)
minuteoptional [default value is 0]the minute of the hour (0 - 59)
secondoptional [default value is 0]the second of the minute (0 - 59)
millisecondoptional [default value is 0]the millisecond of the second (0 - 999)
microsecondoptional [default value is 0]the microsecond of the millisecond (0 - 999)


✐ Examples

1 Create a DateTime object for 10:30 AM, May 3, 2024, UTC time zone

In this example,

  1. We use the DateTime.utc() constructor to create a DateTime object representing 10:30 AM, May 3, 2024, in the UTC time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var dateTime = DateTime.utc(2024, 5, 3, 10, 30, 0);
  print(dateTime);
}

Output

2024-05-03 10:30:00.000Z

2 Create a DateTime object for 10:30 AM, May 3, 2024, UTC time zone

In this example,

  1. We use the DateTime.utc() constructor to create a DateTime object representing 10:30 AM, May 3, 2024, in the UTC time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var dateTime = DateTime.utc(2024, 5, 3, 10, 30, 0);
  print(dateTime);
}

Output

2024-05-03 10:30:00.000Z

3 Create a DateTime object for 10:30 AM, May 3, 2024, UTC time zone

In this example,

  1. We use the DateTime.utc() constructor to create a DateTime object representing 10:30 AM, May 3, 2024, in the UTC time zone.
  2. We print the DateTime object to standard output.

Dart Program

void main() {
  var dateTime = DateTime.utc(2024, 5, 3, 10, 30, 0);
  print(dateTime);
}

Output

2024-05-03 10:30:00.000Z

Summary

In this Dart tutorial, we learned about DateTime.utc constructor of DateTime: the syntax and few working examples with output and detailed explanation for each example.