How to convert DateTime into different timezones? The DateTime class has two methods .toLocal() and .toUtc(). But if I want to display time in another time zone. How can I do it?
-
***[LOOK](http://stackoverflow.com/a/18021707/645128)*** here (references Java script) – ryyker Oct 08 '14 at 13:00
9 Answers
DateTime
doesn't contain timezone information therefore you can't create a DateTime
in a specific timezone only the timezone of your system and UTC are available.
You can wrap the DateTime in a custom class and add timezone information to the wrapper. You also need a table of offsets for each timezone and then add/substract the offset from the UTC date.

- 623,577
- 216
- 2,003
- 1,567
-
4A new package popped up at pub.dartlang.org that seems to do that: https://pub.dartlang.org/packages/timezone – Günter Zöchbauer Oct 17 '14 at 06:12
-
2
-
1
Here is my solution for EST time zone but you can change it to any other
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
extension DateTimeExtension on DateTime {
static int _estToUtcDifference;
int _getESTtoUTCDifference() {
if (_estToUtcDifference == null) {
tz.initializeTimeZones();
final locationNY = tz.getLocation('America/New_York');
tz.TZDateTime nowNY = tz.TZDateTime.now(locationNY);
_estToUtcDifference = nowNY.timeZoneOffset.inHours;
}
return _estToUtcDifference;
}
DateTime toESTzone() {
DateTime result = this.toUtc(); // local time to UTC
result = result.add(Duration(hours: _getESTtoUTCDifference())); // convert UTC to EST
return result;
}
DateTime fromESTzone() {
DateTime result = this.subtract(Duration(hours: _getESTtoUTCDifference())); // convert EST to UTC
String dateTimeAsIso8601String = result.toIso8601String();
dateTimeAsIso8601String += dateTimeAsIso8601String.characters.last.equalsIgnoreCase('Z') ? '' : 'Z';
result = DateTime.parse(dateTimeAsIso8601String); // make isUtc to be true
result = result.toLocal(); // convert UTC to local time
return result;
}
}

- 363
- 3
- 12
I wrote a package for this. It's called Instant, and it can convert a DateTime in any given timezone worldwide. Take a detailed look at https://aditya-kishore.gitbook.io/instant/
The basic usage for converting a DateTime to a timezone is very simple:
//Assumes Instant is in your pubspec
import 'package:instant/instant.dart';
//Super Simple!
DateTime myDT = DateTime.now(); //Current DateTime
DateTime EastCoast = dateTimeToZone(zone: "EST", datetime: myDT); //DateTime in EST zone
return EastCoast;
This works with one line of code and minimal hassle.

- 504
- 3
- 16
-
-
-
Have you imported the instant library? It's definitely still available (https://pub.dev/documentation/instant/latest/instant/instant-library.html) – AKushWarrior May 17 '21 at 15:23
-
Note that the instant package is not daylight saving's time aware and it will return incorrect values assuming -5 from UTC year-round. This is a non-starter. The better approach is to use the "timezone" package similar to @Boris's implementation. – Pat Jul 09 '21 at 16:39
You can use an external package, like: timezone
.
See docs here: https://pub.dev/packages/timezone
Here's a sample code to get the time in Los Angeles (PST/PDT).
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
DateTime _getPSTTime() {
tz.initializeTimeZones();
final DateTime now = DateTime.now();
final pacificTimeZone = tz.getLocation('America/Los_Angeles');
return tz.TZDateTime.from(now, pacificTimeZone);
}

- 334
- 2
- 6
import 'package:timezone/timezone.dart'
String locationLocal = await FlutterNativeTimezone.getLocalTimezone();
//Esta Função recebe uma data/hora e converte para data/hora local.
TZDateTime convertFireBaseToLocal(TZDateTime tzDateTime, String locationLocal) {
TZDateTime nowLocal = new TZDateTime.now(getLocation(locationLocal));
int difference = nowLocal.timeZoneOffset.inHours;
TZDateTime newTzDateTime;
newTzDateTime = tzDateTime.add(Duration(hours: difference));
return newTzDateTime;
}

- 17
- 3
-
3Welcome to SO, it will help if you add some explanation to support your answer. – Sandeep Kumar May 07 '20 at 15:50
-
-
I modified Boris answer to pretend as user is in EST, otherwise time is adjusted to UTC:
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
extension DateTimeExtension on DateTime {
static int? _estToUtcDifference;
int _getESTtoUTCDifference() {
if (_estToUtcDifference == null) {
tz.initializeTimeZones();
final locationNY = tz.getLocation('America/New_York');
tz.TZDateTime nowNY = tz.TZDateTime.now(locationNY);
_estToUtcDifference = nowNY.timeZoneOffset.inHours;
}
return _estToUtcDifference!;
}
DateTime toESTzone() {
DateTime result = toUtc(); // local time to UTC
result = result.add(Duration(hours: _getESTtoUTCDifference()));
// convert UTC to EST and remove ZULU as it is not UTC anymore.
String dateTimeAsIso8601String =
result.toIso8601String().replaceAll('Z', '');
result = DateTime.parse(dateTimeAsIso8601String);
return result;
}
DateTime fromESTzone() {
DateTime result = subtract(
Duration(hours: _getESTtoUTCDifference())); // convert EST to UTC
String dateTimeAsIso8601String = result.toIso8601String();
dateTimeAsIso8601String += dateTimeAsIso8601String.endsWith('Z') ? '' : 'Z';
result = DateTime.parse(dateTimeAsIso8601String); // make isUtc to be true
result = result.toLocal(); // convert UTC to local time
return result;
}
}

- 11
- 1
Convert To IST for example, if not interested to use any non-verified lib in production.
DateTime.now().toUtc().add(const Duration(hours: 5, minutes: 30));

- 996
- 15
- 25
use simple EPOC time istead of other stuff
var now = DateTime.now().millisecondsSinceEpoch;

- 22
- 4
-
How does this answer the question? Having epoch is not enough to covert into different timezones. – yardstick17 Feb 18 '22 at 14:24
You can use TimeZoneInfo.ConvertTime() to change timezone. Try like this
DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);
try {
TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
}
catch (TimeZoneNotFoundException) {
Console.WriteLine("Timezone not found");
}
catch (InvalidTimeZoneException) {
Console.WriteLine("Invalid Timezone");
}
This will convert from Hawaiian Standard Time to Local.
It is just an example. Use it to convert as per your need.

- 623,577
- 216
- 2,003
- 1,567

- 105
- 3
-
1
-
-
Here is an old discussion on how to implement it yourself: https://groups.google.com/a/dartlang.org/forum/?fromgroups#!searchin/misc/offsetdate/misc/9RXTZSDDE9E/tIHOvmGXXo8J – Florian Loitsch Oct 09 '14 at 07:26
-
this is for C#, the owner of question is asking for dart/flutter – Alberto Monteiro Feb 14 '20 at 11:32