ZetCode

Dart DateTime

last modified January 28, 2024

In this article we show how to work with date and time in Dart language.

A DateTime is an instance in time. It can represent represent time values that are at a distance of at most 100 000 000 days from epoch (1970-01-01 UTC): -271821-04-20 to 275760-09-13.

Dart today's date

In the first example, we get today's date.

main.dart
void main() {
  var now = new DateTime.now();
  print(now);
}

The current datetime is returned with DateTime.now.

$ dart main.dart
2023-01-27 18:44:06.245959

Dart DateTime parts

We have properties to get the parts of the datetime.

main.dart
void main() {
  var now = DateTime.now();

  print("Year is ${now.year}");
  print("Month is ${now.month}");
  print("Day is ${now.day}");
  print("Hour is ${now.hour}");
  print("Minutes is ${now.minute}");
  print("Second is ${now.second}");
}

The program prints the year, month, day, hour, minute, and second parts of the current datetime.

$ dart main.dart
Year is 2023
Month is 1
Day is 27
Hour is 18
Minutes is 46
Second is 46

Dart UTC time

The UTC (Universal Coordinated time) was chosen to be the primary time standard. UTC is used in aviation, weather forecasts, flight plans, air traffic control clearances, and maps. Unlike local time, UTC does not change with a change of seasons.

main.dart
void main() {
  var now = new DateTime.now();
  print(now);
  print(now.isUtc);

  print('---------------------');

  var utc = now.toUtc();
  print(utc);
}

The toUtc method returns the datetime value in the UTC time zone. With isUtc method we can check if the datetime value is in UTC.

$ dart main.dart
2023-01-27 18:49:59.321712
false

Dart Unix time

The Unix time is the number of seconds since the Unix epoch (1970-01-01T00:00:00Z UTC). The Unix time is widely used in computing.

main.dart
void main() {
  var now = new DateTime.now();
  print(now.microsecondsSinceEpoch);
}

The microsecondsSinceEpoch method returns the number of microseconds since the Unix epoch.

$ dart unix_time.dart
1674841938541171

Dart DateTime parse

The parse method constructs a DateTime object from a string. The function parses a subset of ISO 8601.

main.dart
void main() {
  var pattern = "2021-10-07";

  DateTime dt = DateTime.parse(pattern);
  print(dt);
}

In the example, we parse a datetime object from a string.

Dart format DateTime

We use the intl package to formate datetime.

$ dart pub add intl

We add the package.

main.dart
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() {
  var now = DateTime.now();
  print(now);

  String pattern = 'yyyy-MM-dd';
  String formatted = DateFormat(pattern).format(now);
  print(formatted);

  print(DateFormat.yMMMMd('en_US').format(now));

  initializeDateFormatting('sk_SK', null)
      .then((_) => print(DateFormat.yMMMMd('sk_SK').format(now)));
}

In the program, we format the current datetime.

String pattern = 'yyyy-MM-dd';
String formatted = DateFormat(pattern).format(now);
print(formatted);

We format the datetime with DateFormat class.

initializeDateFormatting('sk_SK', null)
    .then((_) => print(DateFormat.yMMMMd('sk_SK').format(now)));

To use formats in different locales, we use the initializeDateFormatting method.

$ dart main.dart
2023-01-27 18:59:47.619085
2023-01-27
January 27, 2023
27. januára 2023

Dart add and subtract DateTime

DateTime contains methods for doing time arithmetic operations.

main.dart
void main() {
  DateTime dt = new DateTime(2019, 2, 22, 14, 0, 0);

  var dt1 = dt.add(Duration(days: 5));
  var dt2 = dt.add(Duration(days: 5, hours: 23, seconds: 54));
  var dt3 = dt.subtract(Duration(days: 56));

  print(dt1);
  print(dt2);
  print(dt3);
}

The program adds and subtracts durations to and from the specified datetime object.

$ dart main.dart
2019-02-27 14:00:00.000
2019-02-28 13:00:54.000
2018-12-28 14:00:00.000

The Borodino Battle

The difference method returns a duration object between two datetimes.

main.dart
void main() {
  var now = DateTime.now();
  var borodino_battle = DateTime(1812, 9, 7);

  var diff = now.difference(borodino_battle).inDays;

  print("$diff days have passed since the Battle of Borodino");
}

In the example, we compute the number of days passed since the Borodino battle.

$ dart borodino.dart
76843 days have passed since the Battle of Borodino

Source

DateTime - language reference

In this article we worked with date and time in Dart.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Dart tutorials.