我有一个包含日期的字符串,格式为 yyyyMMddHHmmss(例如)(20180626170555),我正在使用以下代码将其转换为日期时间
dateTimeFromString(json['dateTime'], "yyyyMMddHHmmss")
异常(exception)是:
FormatException: Trying to read MM from 20180623130424 at position 14
可能是什么原因?
最佳答案
DateTime.parse("string date here")
只接受一些格式化的字符串。检查以下接受字符串的示例。
“2012-02-27 13:27:00”
“2012-02-27 13:27:00.123456789z”
“2012-02-27 13:27:00,123456789z”
“20120227 13:27:00”
“20120227T132700”
“20120227”
“+20120227”
“2012-02-27T14Z”
“2012-02-27T14+00:00”
“-123450101 00:00:00 Z”
:在-12345年。“2002-02-27T14:00:00-0500”
:同“2002-02-27T19:00:00Z”
=> 字符串到日期时间
DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString);
=> 日期时间到字符串
String date = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());
Reference links:
- Use
intl
forDateFormat
from flutter package (https://pub.dev/packages/intl)- DateTime.parse() => https://api.dart.dev/stable/2.7.2/dart-core/DateTime/parse.html
关于dart - 无法将 yyyyMMddHHmmss 格式的字符串日期转换为 DateTime dart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042621/