Dart DateTime toLocal()
Syntax & Examples


Syntax of DateTime.toLocal()

The syntax of DateTime.toLocal() method is:

 DateTime toLocal() 

This toLocal() method of DateTime returns this DateTime value in the local time zone.

Return Type

DateTime.toLocal() returns value of type DateTime.



✐ Examples

1 Convert UTC date to local date

In this example,

  1. We create a DateTime instance named utcDate representing UTC time '2023-05-01 12:00'.
  2. We then use the toLocal() method to convert it to the local time zone.
  3. The resulting DateTime instance in the local time zone is printed to standard output.

Dart Program

void main() {
  DateTime utcDate = DateTime.utc(2023, 5, 1, 12, 0);
  DateTime localDate = utcDate.toLocal();
  print('Local date: $localDate');
}

Output

Local date: 2023-05-01 17:30:00.000

2 Convert UTC date to local date

In this example,

  1. We create a DateTime instance named utcDate2 representing UTC time '2023-05-01 18:30'.
  2. We then use the toLocal() method to convert it to the local time zone.
  3. The resulting DateTime instance in the local time zone is printed to standard output.

Dart Program

void main() {
  DateTime utcDate2 = DateTime.utc(2023, 5, 1, 18, 30);
  DateTime localDate2 = utcDate2.toLocal();
  print('Local date: $localDate2');
}

Output

Local date: 2023-05-02 00:00:00.000

3 Convert UTC date to local date

In this example,

  1. We create a DateTime instance named utcDate3 representing UTC time '2023-05-01 08:45'.
  2. We then use the toLocal() method to convert it to the local time zone.
  3. The resulting DateTime instance in the local time zone is printed to standard output.

Dart Program

void main() {
  DateTime utcDate3 = DateTime.utc(2023, 5, 1, 8, 45);
  DateTime localDate3 = utcDate3.toLocal();
  print('Local date: $localDate3');
}

Output

Local date: 2023-05-01 14:15:00.000

Summary

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