unable to convert string date in Format yyyyMMddHHmmss to DateTime dart

后端 未结 6 823
不知归路
不知归路 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:02

    You can use DateFormat to parse a DateTime from string to an object

    // With en_US locale by default
    var newDateTimeObj = new DateFormat().add_yMd().add_Hms().parse("7/10/1996 10:07:23")
    // with a defined format
    var newDateTimeObj2 = new DateFormat("dd/MM/yyyy HH:mm:ss").parse("10/02/2000 15:13:09")
    

    Check the doc here.

    0 讨论(0)
  • 2021-02-07 02:02

    to convert from "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" to 'MM/dd/yyyy hh:mm a'

    date = '2021-01-26T03:17:00.000000Z';
    DateTime parseDate =
        new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(date);
    var inputDate = DateTime.parse(parseDate.toString());
    var outputFormat = DateFormat('MM/dd/yyyy hh:mm a');
    var outputDate = outputFormat.format(inputDate);
    print(outputDate)
    

    output 01/26/2021 03:17 AM

    0 讨论(0)
  • 2021-02-07 02:06

    Basic information about how to convert String to Date and Date to string in flutter. Look at below link

    https://quickstartflutterdart.blogspot.com/2018/10/how-to-convert-string-to-date-and-date.html

    Might be it will be helped for others.

    0 讨论(0)
  • 2021-02-07 02:07

    DateTime.parse("string date here") accept some formatted string only. Check below examples of accepted strings.

    • "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": in the year -12345.
    • "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

    => String to DateTime

    DateTime tempDate = new DateFormat("yyyy-MM-dd hh:mm:ss").parse(savedDateString);
    

    => DateTime to String

    String date = DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());
    

    Reference links:

    • Use intl for DateFormat from flutter package (https://pub.dev/packages/intl)
    • DateTime.parse() => https://api.dart.dev/stable/2.7.2/dart-core/DateTime/parse.html
    0 讨论(0)
  • 2021-02-07 02:09

    From the docs, you need Single M to month in year :

    dateTimeFromString(json['dateTime'], "yMdHms")
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题