Converting timestamp

I couldn't find a solution to this, I'm grabbing data from firebase and one of the fields is a timestamp which looks like this -> 1522129071. How to convert it to a date?

Swift example (works) :

func readTimestamp(timestamp: Int) { let now = Date() let dateFormatter = DateFormatter() let date = Date(timeIntervalSince1970: Double(timestamp)) let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth]) let diff = Calendar.current.dateComponents(components, from: date, to: now) var timeText = "" dateFormatter.locale = .current dateFormatter.dateFormat = "HH:mm a" if diff.second! <= 0 || diff.second! > 0 && diff.minute! == 0 || diff.minute! > 0 && diff.hour! == 0 || diff.hour! > 0 && diff.day! == 0 { timeText = dateFormatter.string(from: date) } if diff.day! > 0 && diff.weekOfMonth! == 0 { timeText = (diff.day == 1) ? "\(diff.day!) DAY AGO" : "\(diff.day!) DAYS AGO" } if diff.weekOfMonth! > 0 { timeText = (diff.weekOfMonth == 1) ? "\(diff.weekOfMonth!) WEEK AGO" : "\(diff.weekOfMonth!) WEEKS AGO" } return timeText } 

My attempt at Dart:

String readTimestamp(int timestamp) { var now = new DateTime.now(); var format = new DateFormat('HH:mm a'); var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp); 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); // Doesn't get called when it should be } else { time = diff.inDays.toString() + 'DAYS AGO'; // Gets call and it's wrong date } return time; } 

And it returns dates/times that are waaaaaaay off.

UPDATE:

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; } 
9

20 Answers

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

Change:

var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp); 

to

var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000); 
16
  • From milliseconds:

    var millis = 978296400000; var dt = DateTime.fromMillisecondsSinceEpoch(millis); // 12 Hour format: var d12 = DateFormat('MM/dd/yyyy, hh:mm a').format(dt); // 12/31/2000, 10:00 PM // 24 Hour format: var d24 = DateFormat('dd/MM/yyyy, HH:mm').format(dt); // 31/12/2000, 22:00 
  • From Firestore:

    Map<String, dynamic> map = docSnapshot.data()!; DateTime dt = (map['timestamp'] as Timestamp).toDate(); 
  • Converting one format to other:

    • 12 Hour to 24 Hour:

      var input = DateFormat('MM/dd/yyyy, hh:mm a').parse('12/31/2000, 10:00 PM'); var output = DateFormat('dd/MM/yyyy, HH:mm').format(input); // 31/12/2000, 22:00 
    • 24 Hour to 12 Hour:

      var input = DateFormat('dd/MM/yyyy, HH:mm').parse('31/12/2000, 22:00'); var output = DateFormat('MM/dd/yyyy, hh:mm a').format(input); // 12/31/2000, 10:00 PM 

Use intl package (for formatting)

0

Full code for anyone who needs it:

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; } 

Thank you Alex Haslam for the help!

0

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 you 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.

if anyone come here to convert firebase Timestamp here this will help

Timestamp time; DateTime.fromMicrosecondsSinceEpoch(time.microsecondsSinceEpoch) 
1

To convert Firestore Timestamp to DateTime object just use .toDate() method.

Example:

Timestamp now = Timestamp.now(); DateTime dateNow = now.toDate(); 

As you can see in docs

0

meh, just use library; it does all the heavy-lifting for you.

EDIT:

From your question, it seems you wanted relative time conversions, and the timeago library enables you to do this in 1 line of code. Converting Dates isn't something I'd choose to implement myself, as there are a lot of edge cases & it gets fugly quickly, especially if you need to support different locales in the future. More code you write = more you have to test.

import 'package:timeago/timeago.dart' as timeago; final fifteenAgo = DateTime.now().subtract(new Duration(minutes: 15)); print(timeago.format(fifteenAgo)); // 15 minutes ago print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m print(timeago.format(fifteenAgo, locale: 'es')); // Add a new locale messages timeago.setLocaleMessages('fr', timeago.FrMessages()); // Override a locale message timeago.setLocaleMessages('en', CustomMessages()); print(timeago.format(fifteenAgo)); // 15 min ago print(timeago.format(fifteenAgo, locale: 'fr')); // environ 15 minutes 

to convert epochMS to DateTime, just use...

final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1546553448639); 
4

Just make sure to multiply by the right factor:

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

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); 
3

How to implement:

import 'package:intl/intl.dart'; getCustomFormattedDateTime(String givenDateTime, String dateFormat) { // dateFormat = 'MM/dd/yy'; final DateTime docDateTime = DateTime.parse(givenDateTime); return DateFormat(dateFormat).format(docDateTime); } 

How to call:

getCustomFormattedDateTime('2021-02-15T18:42:49.608466Z', 'MM/dd/yy'); 

Result:

02/15/21

Above code solved my problem. I hope, this will also help you. Thanks for asking this question.

1

Assuming the field in timestamp firestore is called timestamp, in dart you could call the toDate() method on the returned map.

// Map from firestore // Using flutterfire package hence the returned data() Map<String, dynamic> data = documentSnapshot.data(); DateTime _timestamp = data['timestamp'].toDate(); 

I don't know if this will help anyone. The previous messages have helped me so I'm here to suggest a few things:

import 'package:intl/intl.dart'; DateTime convertTimeStampToDateTime(int timeStamp) { var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000); return dateToTimeStamp; } String convertTimeStampToHumanDate(int timeStamp) { var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000); return DateFormat('dd/MM/yyyy').format(dateToTimeStamp); } String convertTimeStampToHumanHour(int timeStamp) { var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000); return DateFormat('HH:mm').format(dateToTimeStamp); } int constructDateAndHourRdvToTimeStamp(DateTime dateTime, TimeOfDay time ) { final constructDateTimeRdv = dateTimeToTimeStamp(DateTime(dateTime.year, dateTime.month, dateTime.day, time.hour, time.minute)) ; return constructDateTimeRdv; } 

Simply call this method to return your desired DateTime value in String.

String parseTimeStamp(int value) { var date = DateTime.fromMillisecondsSinceEpoch(value * 1000); var d12 = DateFormat('MM-dd-yyyy, hh:mm a').format(date); return d12; } 

Example: if you pass the TimeStamp value 1636786003, you will get the result as

11-12-2021, 10:46PM 

If you are here to just convert Timestamp into DateTime,

Timestamp timestamp = widget.firebaseDocument[timeStampfield]; DateTime date = Timestamp.fromMillisecondsSinceEpoch( timestamp.millisecondsSinceEpoch).toDate(); 

I tested this one and it works

// Map from firestore // Using flutterfire package hence the returned data() Map<String, dynamic> data = documentSnapshot.data(); DateTime _timestamp = data['timestamp'].toDate(); 

Test details can be found here:

All of that above can work but for a quick and easy fix you can use the time_formatter package.

Using this package you can convert the epoch to human-readable time.

String convertTimeStamp(timeStamp){ //Pass the epoch server time and the it will format it for you String formatted = formatTime(timeStamp).toString(); return formatted; } //Then you can display it Text(convertTimeStamp['createdTimeStamp'])//< 1 second : "Just now" up to < 730 days : "1 year" 

Here you can check the format of the output that is going to be displayed: Formats

Timestamp has [toDate] method then you can use it directly as an DateTime.

timestamp.toDate(); // return DateTime object 

Also there is an stupid way if you want really convert it:

DateTime.parse(timestamp.toDate().toString()) 

Print DateTime, TimeStamp as string from Firebase Firestore:

 Timestamp t; String s; DateTime d; //Declaring Variables snapshots.data.docs[index]['createdAt'] is Timestamp ? t = snapshots.data.docs[index]['createdAt'] : s = snapshots.data.docs[index]['createdAt'].toString(); //check createdAt field Timestamp or DateTime snapshots.data.docs[index]['createdAt'] is Timestamp ? d = t.toDate() : s = snapshots.data.docs[index]['createdAt'].toString(); print(s.toString()); //Print Date and Time if DateTime print(d.toString()); //Print Date and Time if TimeStamp 

Assuming you have a class

class Dtime { int dt; Dtime(this.dt); String formatYMED() { var date = DateTime.fromMillisecondsSinceEpoch(this.dt); var formattedDate = DateFormat.yMMMMEEEEd().format(date); return formattedDate; } String formatHMA() { var time = DateTime.fromMillisecondsSinceEpoch(this.dt * 1000); final timeFormat = DateFormat('h:mm a', 'en_US').format(time); return timeFormat; } 

I am a beginner though, I hope that works.

Long num format date into Calender format from:

var responseDate = 1637996744; var date = DateTime.fromMillisecondsSinceEpoch(responseDate); //to format date into different types to display; // sample format: MM/dd/yyyy : 11/27/2021 var dateFormatted = DateFormat('MM/dd/yyyy').format(date); // sample format: dd/MM/yyy : 27/11/2021 var dateFormatted = DateFormat('dd/MM/yyyy').format(date); // sample format: dd/MMM/yyyy : 27/Nov/2021 var dateFormatted = DateFormat('dd/MMM/yyyy').format(date); // sample format: dd/MMMM/yyyy : 27/November/2021 var dateFormatted = DateFormat('dd/MMMM/yyyy').format(date); print("Date After Format = $dateFormatted"); 
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; } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like