How do i get the current date and time of places around the world in flutter

1.9k Views Asked by At

i am currently working on a world clock app using flutter, can any one recommend dependencies or API i can integrate into my app that provide a list of places around the world, another issue i am having is getting the app to display current date and time of those selected places

1

There are 1 best solutions below

2
On

The DateTime class of Dart supports UTC and the local time zone. If you want to calculate a timestamp to a different time zone, check out the timezone package on pub.dev.

https://pub.dev/packages/timezone

It supports something like this for example:

import 'package:timezone/standalone.dart' as tz;

Future<void> setup() async {
  await tz.initializeTimeZone();
  var detroit = tz.getLocation('America/Detroit');
  var now = tz.TZDateTime.now(detroit);
}

It also consists of a time zone database:

import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;

void main() {
  tz.initializeTimeZones();
  var locations = tz.timeZoneDatabase.locations;
  print(locations.length); // => 429
  print(locations.keys.first); // => "Africa/Abidjan"
  print(locations.keys.last); // => "US/Pacific"
}