我想将 DateTime.now() 格式化为 2024 年 3 月 19 日上午 11:06:32 UTC+5”,但其数据类型应为 DateTime。
我正在获取该格式的日期,但它是字符串:
String formattedDate = DateFormat("MMMM dd, yyyy 'at' hh:mm:ss a 'UTC+5'").format(DateTime.now().toUtc().add(Duration(hours: 5)));
所以,我想要 DateTime 数据类型中的字符串,但我无法做到。
您可以概括并使用
'UTC'Z
代替 'UTC+5'
,并且为了便于使用,将格式定义为变量
DateFormat specialFormat = DateFormat("MMMM d, yyyy 'at' hh:mm:ss a 'UTC'Z");
现在你需要使用
DateFormat.parse()
方法
try {
String inputString = "March 19, 2024 at 11:06:32 AM UTC+5";
DateTime parsedDate = specialFormat.parse(inputString);
} catch (e) {
print("Error parsing date: $e");
}