【问题标题】:How to convert DateTime to a String [duplicate]如何将 DateTime 转换为字符串 [重复]
【发布时间】:2019-11-28 01:44:12
【问题描述】:

我正在尝试将DateTime 显示为ListTile 中的文本。

我所做的事情给了我信息:

参数类型“String”不能分配给参数类型“DateTime”

代码:

_getDate() {
    String formattedDate = DateFormat(yyyy-MM-dd).format(widget.exampleForDate.myDate);
    return ListTile(
      title: Text(formattedDate),
    );
  }

【问题讨论】:

  • widget.exampleForDate.myDateDateTime 对象还是String
  • 它是一个 DateTime 对象

标签: flutter dart


【解决方案1】:

使用intl包访问DateFormat

DateTime dateTime = widget.exampleForDate.myDate; // your dateTime object
DateFormat dateFormat = DateFormat("yyyy-MM-dd"); // how you want it to be formatted
String string = dateFormat.format(dateTime); // format it 

像这样使用它

ListTile(title: Text(string));

【讨论】:

  • 现在得到消息:“参数类型Timestamp不能分配给参数类型DateTime
  • 你在哪里使用string,应该在ListTile(title: Text(string))
  • 它就像现在给出的答案一样,字符串在 listTile 但我需要显示我的小部件日期而不是当前日期
  • 请查看更新后的答案。
【解决方案2】:

Java 脚本中的简单 Date().toString()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2014-01-15
    • 2021-03-31
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多