可以使用 Dart 中的 DateTime 类来将字符串时间转换为指定的时间格式。以下是一个示例代码:

String dateString = '2022-01-01 12:00:00';
DateTime dateTime = DateTime.parse(dateString);
String formattedDate = DateFormat('yyyy/MM/dd HH:mm:ss').format(dateTime);
print(formattedDate);

在上面的代码中,我们首先定义了一个字符串类型的时间日期,然后使用 DateTime.parse() 方法将其转换为 DateTime 类型的对象。接下来,我们使用 DateFormat 类来指定所需的时间格式,并使用 format() 方法将 DateTime 对象格式化为字符串。最后,我们打印出格式化后的日期字符串。

这里我们使用了 yyyy/MM/dd HH:mm:ss 格式,您可以根据自己的需要使用不同的时间格式。

Flutter 字符串时间格式转换:将字符串转换为指定时间格式

原文地址: https://www.cveoy.top/t/topic/kFtL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录