【问题标题】:How I can show only current time using DateTime.now() in flutter?如何在颤动中使用 DateTime.now() 仅显示当前时间?
【发布时间】:2020-12-25 21:28:59
【问题描述】:
 DateTime currentTime = new DateTime.now();

  Widget CustomCards(Color frontColor, String fetchTime) {
    Card cards = new Card(
      elevation: 1.0,
      margin: const EdgeInsets.symmetric(horizontal: 2.0, vertical: 2.0),
      color: bgColor,
      shape: new RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(15.0),
      ),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(5.0),
            child: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 15.0),
                  child: Text(
                  currentTime.toString(),
                  style: new TextStyle(
                    fontStyle: FontStyle.italic,
                    fontFamily: 'Satisfy',
                    fontSize: 15.0,
                    color: frontColor,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
    return cards;
  }

如何从 DateTime 显示当前时间,因为它始终显示当前日期和时间,但我想以 格式 hh:mm:ss 显示时间。这里秒应该是他们的。我找不到任何逻辑,请帮助我。 谢谢你

【问题讨论】:

标签: android flutter datetime flutter-layout flutter-test


【解决方案1】:

使用 DateFormat 类将其转换为字符串和您想要的模式。

例子:

DateFormat('your date pattern').format(currentTime);

【讨论】:

    【解决方案2】:

    在 pubspec.yaml intl: ^0.16.1 中添加依赖项(检查 pub.dev 中的最新依赖项),然后您可以在 dart 文件中使用 DateFormat.yMMMd().format(DateTime.now());。您可以根据需要更改日期格式的模式。

    对于 DateFormat 集中不存在的自定义日期模式,您可以使用

    DateFormat('hh:mm:ss').format(DateTime.now());
    

    【讨论】:

    • 我只想显示 hh:mm:ss,而不是年份、月份或日期。请给我建议。顺便说一句,谢谢你。
    • @ShîvamYadav 更新了自定义日期模式的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多