如何在 flutter 中使用 DateTime.now() 仅显示当前时间?

Shî*_*dav 2 datetime android flutter flutter-test flutter-layout

 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;
  }
Run Code Online (Sandbox Code Playgroud)

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

Nik*_*Nik 10

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

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

DateFormat('hh:mm:ss').format(DateTime.now());
Run Code Online (Sandbox Code Playgroud)