How to Convert timestamp in Flutter?

· 5 min read
How to Convert TimeStamp In Flutter
How to Convert TimeStamp In Flutter

Dates are most commonly used within so many Flutter Mobile applications. While the end-users are always interested in showing date in a meaningful and readable format. So in this article, we will learn about How to Convert timestamp in Flutter or Dart.

You can also read our article on how to add DateTime Picker Widget and Step By Step Guide to integrate a Firebase in Flutter Mobile Application.

How to convert timestamp to Date?

Generally, while grabbing data from Firebase it returns a Date into timestamp format.

Depending on the type of Date format received back from firebase users can convert it into Date format.

My current code Snippet looks like below:

String readTimestamp(int timestamp) {
    var now = new DateTime.now();
    var format = new DateFormat('HH:mm a');
    var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
    var diff = date.difference(now);
    var time = '';

    if (diff.inSeconds <= 0 || diff.inSeconds > 0 && diff.inMinutes == 0 || diff.inMinutes > 0 && diff.inHours == 0 || diff.inHours > 0 && diff.inDays == 0) {
      time = format.format(date);
    } else {
      if (diff.inDays == 1) {
        time = diff.inDays.toString() + 'DAY AGO';
      } else {
        time = diff.inDays.toString() + 'DAYS AGO';
      }
    }

    return time;
  }

Our timestamp format is in fact in Seconds Unix timestamp as opposed to microseconds. If so the answer is as follows:

We need to make Change in our code like below:

var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);

to

var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

The complete code snippet will look like below:

String readTimestamp(int timestamp) {
    var now = DateTime.now();
    var format = DateFormat('HH:mm a');
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    var diff = now.difference(date);
    var time = '';

    if (diff.inSeconds <= 0 || diff.inSeconds > 0 && diff.inMinutes == 0 || diff.inMinutes > 0 && diff.inHours == 0 || diff.inHours > 0 && diff.inDays == 0) {
      time = format.format(date);
    } else if (diff.inDays > 0 && diff.inDays < 7) {
      if (diff.inDays == 1) {
        time = diff.inDays.toString() + ' DAY AGO';
      } else {
        time = diff.inDays.toString() + ' DAYS AGO';
      }
    } else {
      if (diff.inDays == 7) {
        time = (diff.inDays / 7).floor().toString() + ' WEEK AGO';
      } else {

        time = (diff.inDays / 7).floor().toString() + ' WEEKS AGO';
      }
    }

    return time;
  }

Users can also make use of  intl package,

import 'package:intl/intl.dart';

Then

int timeInMillis = 1586348737122;
var date = DateTime.fromMillisecondsSinceEpoch(timeInMillis);
var formattedDate = DateFormat.yMMMd().format(date); // Apr 8, 2020

If you are using firestore and not just storing the timestamp as a string a date field in a document will return a Timestamp. The Timestamp object contains a toDate() method.

Using timeago user can create a relative time quite simply:

_ago(Timestamp t) {
  return timeago.format(t.toDate(), 'en_short');
}

build() {
  return  Text(_ago(document['mytimestamp'])));
}

Make sure to set _firestore.settings(timestampsInSnapshotsEnabled: true); to return a Timestamp instead of a Date object.

Just make sure to Multiply by the right factor:

If the TimeStamp arrived back from Firebase is in the MicroSecond Format then

  • Micro: multiply by 1000000 (which is 10 power 6)

If the TimeStamp arrived back from Firebase is in the MilliSecond Format then

  • Milli: multiply by 1000 (which is 10 power 3)

This is what it should look like in Dart:

var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);

or

var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

Conclusion:

In this article, we have been through How to Convert timestamp in Flutter Mobile Application.

Still, need support for Flutter Mobile Application Development? We Would love to assist you.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Leave a Reply