问题描述
我有一个包含日期的字符串,格式为yyyyMMddHHmmss(例如)(20180626170555),并且我正在使用以下代码将其转换为日期时间
dateTimeFromString(json ['dateTime'], yyyyMMddHHmmss)
例外是:
FormatException:尝试从位置14
$ p $读取20180623130424的MM p>
是什么原因?
解决方案
intl DateFormat
无法处理您的输入字符串,因为它没有任何分隔符。整个字符串将作为年份消耗掉。但是DateTime.parse
确实可以解决这个问题(几乎)。碰巧恰好期望您拥有(再次接近)格式。
解析
可接受的样式之一是20120227T132700
,只是与T
日期/时间分隔符不同。
尝试一下:
字符串日期='20180626170555';
字符串dateWithT = date.substring(0,8)+‘T’+ date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
i have a string containing date in format yyyyMMddHHmmss (e.g.) (20180626170555) and i am using following code to convert it into date time
dateTimeFromString(json['dateTime'], "yyyyMMddHHmmss")
exception is:
FormatException: Trying to read MM from 20180623130424 at position 14
what can be the reason?
解决方案
intl DateFormat
can't cope with your input string as it doesn't have any separators. The whole string gets consumed as the year. HoweverDateTime.parse
does cope with this (nearly). It happens to expect precisely the format you have (again, nearly).One of the acceptable styles to
parse
is20120227T132700
, which just differs by theT
date/time separator.Try this:
String date = '20180626170555'; String dateWithT = date.substring(0, 8) + 'T' + date.substring(8); DateTime dateTime = DateTime.parse(dateWithT);
这篇关于无法将格式yyyyMMddHHmmss的字符串日期转换为DateTime dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!