无法将格式yyyyMMddHHmmss的字符串日期转换为DateTime dart [英] unable to convert string date in Format yyyyMMddHHmmss to DateTime dart

查看:222
本文介绍了无法将格式yyyyMMddHHmmss的字符串日期转换为DateTime dart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期的字符串,格式为yyyyMMddHHmmss(例如)(20180626170555),并且我正在使用以下代码将其转换为日期时间

  dateTimeFromString(json ['dateTime'], yyyyMMddHHmmss)

例外是:

  FormatException:尝试从位置14 


是什么原因?

解决方案

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. However DateTime.parse does cope with this (nearly). It happens to expect precisely the format you have (again, nearly).

One of the acceptable styles to parse is 20120227T132700, which just differs by the T 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆