How to Convert Current Date and Time to UTC in Flutter

In this short Flutter tutorial, let’s check how to convert current time to UTC (Coordinated Universal Time) in Flutter easily.

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

var now = new DateTime.now();

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

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

Following is the complete example.

import 'package:flutter/material.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter DateTime to UTC',
      home: DateExample(),
    );
  }
}


class DateExample extends StatefulWidget {
  DateExample({Key key}) : super(key: key);

  @override
  _DateExampleState createState() => _DateExampleState();
}

class _DateExampleState extends State<DateExample> {

@override
  void initState() {
    super.initState();
    print(DateTime.now().toUtc());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
       body: Center(child: Text('Flutter Current UTC Tutorial'),),
    );
  }
}



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

Leave a Comment

Your email address will not be published. Required fields are marked *