How to Convert Current Date and Time to UTC in Flutter

UTC is a widely used standard for representing the time and ensures that your applications are functioning correctly across different time zones. In this tutorial, we will be exploring the process of converting the current date and time to the Coordinated Universal Time (UTC) format in Flutter.

We use DateTime class for this purpose. You can get the current date and time as given below.

var now = new DateTime.now();

We can use the function toUtc to convert the current date and time to UTC.

var utc = DateTime.now().toUtc()

Most of the time, you may also want to show UTC time in a specific format. You can use the intl package for this.

You can install it using the following command.

flutter pub add intl

Following is the complete example where I convert the current DateTime and show UTC date time as Text.

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          useMaterial3: true,
        ),
        home: const MyHomePage());
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text(
            'Flutter UTC Date Time Example',
          ),
        ),
        body: Center(
            child: Text(DateFormat("MM-dd-yyyy hh:mm")
                .format(DateTime.now().toUtc()))));
  }
}

You will get the following output.

flutter date time utc

If you want to convert the DateTime object to a string then go through this blog post. I hope this Flutter tutorial will be helpful for you.

Leave a Comment