Flutter - Get Device Timezone Examples

This tutorial shows you how to get the device timezone using Flutter.

If you create an application using Flutter and you need to get the timezone of the device that runs the application, it can be done in an easy way.

Get Timezone using DateTime

In Flutter, if you create a DateTime instance using DateTime.now constructor, the created time will be in the local time zone of the device. From the DateTime instance, you can extract the timeZoneName and timeZoneOffset property.

The timeZoneName property contains the time zone name provided by the operating system. As a result, the returned value can be different depending on the operating system. For example, web browsers or Unix-like systems usually return abbreviations, such as "CET" or "CEST". Meanwhile, Windows returns the full name such as "Pacific Standard Time".

The timeZoneOffset property contains the offset time of the time zone. It's the time difference with the UTC time. The type of the property is Duration. If you want to display the Duration, you can read our tutorial about how to format Duration in Dart.

  DateTime dateTime = DateTime.now();
  String timeZoneName = dateTime.timeZoneName;
  Duration timeZoneOffset = dateTime.timeZoneOffset;

Full Code

  import 'dart:io';
  
  import 'package:flutter/material.dart';
  import 'package:safe_device/safe_device.dart';
  
  void main() => runApp(const MyApp());
  
  class MyApp extends StatelessWidget {
  
    const MyApp({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        title: 'Woolha.com Flutter Tutorial',
        home: GetDeviceTimezone(),
      );
    }
  }
  
  class GetDeviceTimezone extends StatefulWidget {
  
    const GetDeviceTimezone({Key? key}) : super(key: key);
  
    @override
    State<StatefulWidget> createState() {
      return _GetDeviceTimezoneState();
    }
  }
  
  class _GetDeviceTimezoneState extends State<GetDeviceTimezone> {
  
    String? _timeZoneName;
    Duration? _timeZoneOffset;
  
    @override
    void initState() {
      super.initState();
      _checkIsRootedOrJailBroken();
    }
  
    void _checkIsRootedOrJailBroken() async {
      DateTime dateTime = DateTime.now();
      String timeZoneName = dateTime.timeZoneName;
      Duration timeZoneOffset = dateTime.timeZoneOffset;
  
      setState(() {
        _timeZoneName = timeZoneName;
        _timeZoneOffset = timeZoneOffset;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Woolha.com Flutter Tutorial'),
          backgroundColor: Colors.teal,
        ),
        body: SizedBox(
          width: double.infinity,
          child: Center(
            child: Text('Timezone: $_timeZoneName, Offset: $_timeZoneOffset'),
          ),
        ),
      );
    }
  }

Summary

Getting the time zone using Flutter is quite simple. You just need to create a DateTime instance. Then, you can get the timeZoneName and the timeZoneOffset properties.

You can also read about: