unable to convert string date in Format yyyyMMddHHmmss to DateTime dart

后端 未结 6 834
不知归路
不知归路 2021-02-07 01:49

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(         


        
6条回答
  •  梦谈多话
    2021-02-07 02:18

    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);
    

提交回复
热议问题