Flutter 如何用格式“解析日期时间”;年月日;飘飘然
到目前为止,我尝试了新的日期格式(“MM/dd/yy”)。解析(“04/03/20”)但它会生成04/03/0020日期。您必须使用datetime类的Format方法格式化此日期。但是,您需要使用parse方法将字符串转换为datetime,因为datetime格式将datatime作为参数 以下行帮助您实现期望输出:Flutter 如何用格式“解析日期时间”;年月日;飘飘然,flutter,dart,Flutter,Dart,到目前为止,我尝试了新的日期格式(“MM/dd/yy”)。解析(“04/03/20”)但它会生成04/03/0020日期。您必须使用datetime类的Format方法格式化此日期。但是,您需要使用parse方法将字符串转换为datetime,因为datetime格式将datatime作为参数 以下行帮助您实现期望输出: print(DateFormat("MM/dd/yy") .format(DateFormat("MM/dd/yy").parse('04/03/20')))
print(DateFormat("MM/dd/yy")
.format(DateFormat("MM/dd/yy").parse('04/03/20')));
更新:
以下代码将给出您预期的结果,但很难确定它是1920年还是2020年
DateTime _dateTime = DateFormat("mm/dd/yy").parse('04/03/20');
_dateTime = DateTime(2000 + _dateTime.year, _dateTime.month, _dateTime.day);
print(_dateTime);
你怎么知道那个日期是指2020年还是1920年?我真正需要的是DateTime对象。请注意,如果您打印(DateFormat(“MM/dd/yyyy”).format(DateFormat(“MM/dd/yy”).parse(“04/03/20”)),结果是相同的错误值“04/03/0020”,但我需要“04/03/2020”,您如何判断该日期是指2020年还是1920年?正如@jamescasia所提到的。你每次都想要20块吗?