简体   繁体   English

如何将时间戳从 firebase 转换为 DateTime 和 flutter

[英]How to convert Timestamp to DateTime from firebase with flutter

I have Timestamp item in firebase. I get the item from dart code timestamp type.我在 firebase 中有时间戳项目。我从 dart 代码时间戳类型中获取该项目。 It shows like 'Timestamp(seconds=1590903768, nanoseconds=26999000)' as it is.它显示为“Timestamp(seconds=1590903768, nanoseconds=26999000)”。

I would like to show on my Application only Date like '2020-06-01' or '06-01'.我想在我的申请中显示日期,例如“2020-06-01”或“06-01”。

Please give me advice.请给我建议。

Timestamp class has a toDate function that converts it to a DateTime object. Timestamp class 有一个toDate function 将其转换为一个DateTime object。 See this for more information.有关更多信息,请参阅 Any formatting you want to do in converting it to a string can be done more easily now with intl package formatters.现在,使用intl package 格式化程序可以更轻松地完成将其转换为字符串的任何格式化。

Example:例子:

Timestamp stamp = Timestamp.now();
DateTime date = stamp.toDate();

You get back a map from firestore.您从 Firestore 取回 map。 Assuming you have named it "creationDate", you can convert the returned timestamp to a DateTime object using the toDate() dart function.假设您将其命名为“creationDate”,您可以使用 toDate() dart function 将返回的时间戳转换为 DateTime object。 The "simplest" way to format the datetime is to use the intl package格式化日期时间的“最简单”方法是使用intl package

// Map From firestore
Map data = documentSnapshot.data();
var creationDate = data['creationDate'].toDate();

//Format using intl package
DateFormat _dateFormat = DateFormat('y-MM-d');
String formattedDate =  _dateFormat.format(dateTime);

You can convert Timestamp DataType to DateTime then formate the DateTime according to your requirements.您可以将Timestamp DataType 转换为DateTime ,然后根据您的要求格式化 DateTime。

Example例子

add this following method in your dart file.在您的 dart 文件中添加以下方法。

String formatTimestamp(Timestamp timestamp) {
  var format = new DateFormat('y-MM-d'); // 'hh:mm' for hour & min
  return format.format(timestamp.toDate());
}

then get the TimeStamp from FireStore and pass it into this method然后从 FireStore 获取时间戳并将其传递给此方法

print(formatTimestamp(doc.data['timestamp']));

OutPut OutPut

2021/01/11

I hope this will be helpfull !!我希望这会有所帮助!

If you want Hours and minutes(hh:mm) from timestamp, Simply create following method which gives you string in return.如果您想要时间戳中的小时和分钟 (hh:mm) ,只需创建以下方法,它会为您提供字符串作为回报。

String convertTimestampToHm(Timestamp timestamp) {
var format = DateFormat('Hm'); // convert into hours and minutes
return format.format(timestamp.toDate());

} }

NOTE: - You can always use Skeleton base on requiremrnt.注意: - 您始终可以根据 requiremrnt 使用骨架

There are different ways to convert TimeStamp to DateTime which differs based on the scenario.TimeStamp to DateTime的方法因场景而异。 I have summarize all the possible ways, Pick the one which suits your case !!我已经总结了所有可能的方法,选择适合您情况的方法!


Conversion of Firebase timestamp to DateTime :Firebase timestamp转换为DateTime

  1.  document['timeStamp'].toDate()
  2.  (document["timeStamp"] as Timestamp).toDate()
  3.  DateTime.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch);
  4.  Timestamp.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch).toDate();
  5. If timeStamp is in microseconds use:如果timeStampmicroseconds为单位,请使用:

     DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
  6. If timeStamp is in milliseconds use:如果timeStampmilliseconds ,请使用:

     DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
  7. Add the following function in your dart file.在您的 dart 文件中添加以下 function。

     String formatTimestamp(Timestamp timestamp) { var format = new DateFormat('yyyy-MM-dd'); // <- use skeleton here return format.format(timestamp.toDate()); }

    call it as formatTimestamp(document['timestamp'])将其称为formatTimestamp(document['timestamp'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM