当前位置: 首页 > 知识库问答 >
问题:

时间戳到日期时间转换Firebase到Flutter

文寒
2023-03-14

     @JsonSerializable()
      class CategoryEntity extends Equatable {
       final String id;
       final String name;
       @JsonKey(fromJson: _fromJson, toJson: _toJson)
       final DateTime createdDate;
       @JsonKey(fromJson: _fromJson, toJson: _toJson)
       final DateTime updatedDate; 
       final String description;

    const CategoryEntity(
      this.id,
      this.name,
      this.createdDate,
      this.updatedDate,
      this.description,
    );

但问题是我无法将数据从Firebase转换为Local。save方法将datecreate转换为Timestamp fine。

     static DateTime _fromJson(int int) =>
        DateTime.fromMillisecondsSinceEpoch(int);
      static int _toJson(DateTime time) => time.millisecondsSinceEpoch;

任何解决办法

共有1个答案

蒋乐意
2023-03-14

根据我的理解,您希望在本地时间向用户显示ServerTimeStamp。

我有两种方法可以检查,

一个使用这个包的timeformatter,将显示您的时间根据用户的时间。

A mini truth table for the format of the value returned by the function:
< 1 second : "Just now"
< 60 seconds : "X seconds" (2-59)
< 2 minutes : "1 minute"
< 60 minutes : "X minutes" (2-59)
< 2 hours : "1 hour"
< 24 hours : "X hours" (2-23)
< 2 days : "1 day"
< 7 days : "X days" (2-6)
< 2 weeks : "1 week"
< 28 days : "X weeks" (2-3)
< 30.44 * 1.5 days : "1 month"
< 365 - 15.22 days : "X months" (2-12)
< 730 days : "1 year"
Rest: "X years"
import 'package:intl/intl.dart';

int timeInMillis = 1586348737122; //no need to convert it just set the timestamp here.
var date = DateTime.fromMillisecondsSinceEpoch(timeInMillis);
var formattedDate = DateFormat.yMMMd().format(date); //
 类似资料:
  • 我正在为我的聊天应用程序使用firebase。在聊天对象中,我使用方法添加时间戳。 请就这个问题提出解决办法

  • 问题内容: 为了获得JavaScript的时间戳,我们使用 到目前为止,将其转换为Python的正确方法是什么?我使用以下代码 我将时间戳除以1000,因为出现类似错误 苏丹。 问题答案: 您当前的方法是正确的,必须除以1000,因为JavaScript返回的时间戳以毫秒为单位,并且期望时间戳以秒为单位。 要保留毫秒精度,可以除以,因此您使用浮点除法而不是整数除法:

  • 问题内容: 如何将Unix时间戳中的时间转换为正常时间? 问题答案: 您的问题含糊不清。我将消除时区的歧义。 如何将Unix时间戳中的时间转换为正常时间? 我怀疑您是以某种方式从数据库而不是从中获取或一个值。在JDBC中,通常希望使用适当的方法来获取特定于DB的数据类型。可以获取MySQL 数据类型,从而为您提供,而后者又是的子类。 简而言之,应该执行以下操作: 要将其呈现给最终用户时,要以人类可

  • 我已经尝试从firebase时间戳中获取日期和时间,如下所示: 但我得到的结果是:

  • 我试图找出如何在Kotlin中将转换为,这在Java中非常简单,但我在Kotlin中找不到任何等效的。 例如:历元时间戳(1970-01-01以来的秒数)== 在Kotlin中是否有解决方案,或者我是否必须在Kotln中使用Java语法?请给我一个简单的例子来说明如何解决这个问题。提前谢谢。 这个链接不是我问题的答案

  • 我在火炉里有时间戳项目。我从省道代码时间戳类型中获取项。它就像是'timestamp(seconds=1590903768,nanoseconds=26999000)'一样。