skip to Main Content

I have a string that contains a string. I am using DateTime.parse() to parse the date but it is not working.

Here is the code:

DateTime eventDate = DateTime.parse(contractDateController.text);

The string in the controller is this: "Mon 01-08-2024"

The error I am getting is this:

Unhandled exception:
FormatException: Invalid date format
Mon  01-08-2024
#0      DateTime.parse (dart:core/date_time.dart:358:7)
#1      _TransactionDetailScreenState.Eval ()
#2      _TransactionDetailScreenState.build.<anonymous closure> (package:deal_diligence/screens/transaction_detail_screen.dart:1958:44)
#3      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1154:21)
#4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:275:24)
#5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:654:11)
#6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:311:5)
#7      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:281:7)
#8      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:167:27)
#9      GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:492:20)
#10     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:468:22)
#11     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:333:11)
#12     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:413:7)
#13     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:376:5)
#14     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:323:7)
#15     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:292:9)
#16     _invoke1 (dart:ui/hooks.dart:186:13)
#17     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:424:7)
#18     _dispatchPointerDataPacket (dart:ui/hooks.dart:119:31)

#0      DartDebugAdapter.evaluateRequest (package:dds/src/dap/adapters/dart.dart:997:7)
<asynchronous suspension>
#1      BaseDebugAdapter.handle (package:dds/src/dap/base_debug_adapter.dart:141:7)
<asynchronous suspension>

What am I doing wrong with this code?
Thanks for your help

2

Answers


  1. If you look at the documentation of DateTime.parse, https://api.flutter.dev/flutter/dart-core/DateTime/parse.html , you’ll see the format of the "accepted strings". Your format has the order of 3-letter-version-of day year-month-day; that is "wrong" for that method, meaning in the wrong order (you have ‘EEE dd-MM-yyyy’ and the necessary format for that parse method is without the name of the day and the other way round, like ‘yyyy-MM-dd’ representing ‘2012-02-27’).

    If you use DateFormat from the intl package, see https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html , you can parse your date. For example this prints out "1" correctly:

    print(DateFormat("EEE dd-MM-yyyy").parse('Mon 01-08-2024').day);
    

    You might also want to look into the tryParse method, https://pub.dev/documentation/intl/latest/intl/DateFormat/tryParse.html , if you are not sure about the format your String will be in.

    Login or Signup to reply.
  2. The string "Mon 01-08-2024" that you are passing from your controller is not the correct format expected by the Datetime.parse(). The format should be in the ISO8601 format. Read the documentation here: https://api.dart.dev/stable/3.2.4/dart-core/DateTime/parse.html

    That being said, you need to provide a format that matches your input string. In your case, you can use the intl package to parse to this format. Here is the link to it: https://pub.dev/packages/intl. You can read its documentation and see that the DateFormat class in the package will accept a string and then parse it accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search