title: How to handle DateTime overflow in Dart / Flutter?
body: <p>In a Flutter project I have an input form with a date picker. When a user picked a date, it is possible to alter the picked date by typing. I want to validate the form input and make sure the date input is a correct date.</p>
  
  <p>Here is a screenshot of the input form: </p>
  
  <p><a href="https://i.stack.imgur.com/DiTVp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DiTVp.png" alt="enter image description here"></a></p>
  
  <p>Unfortunately, the Dart language <code>DateTime</code> object allows overflows. So, giving an input of <code>2019-13-1</code> (DateTime field is formatted like <code>yyyy-MM-dd</code>) and then validating that input will pass the validation, giving back a Date of <code>2020-1-1</code>. The 13th month, due to Dart Date overflow behavior turns into the 1st month, next year.</p>
  
  <p>I know there is a Github issue for this already: <a href="https://github.com/dart-lang/sdk/issues/11189" rel="nofollow noreferrer">https://github.com/dart-lang/sdk/issues/11189</a></p>
  
  <p><strong>Is there anybody who has a working solution to get around this issue?</strong> Thanks for the help in advance!</p>
  
  <p>Code for the DateTimeField is shown below:</p>
  
  <pre><code>                  ListTile(
                      leading: Icon(Icons.calendar_today, size: 21),
                      title: DateTimeField(
                        decoration: InputDecoration(
                          hintText: 'Date'
                        ),
                        autovalidate: false,
                        format: DateFormat('yyyy-MM-dd'),
                        onShowPicker: (context, currentValue) {
                          return showDatePicker(
                            firstDate: DateTime(2019),
                            initialDate: DateTime.now(),
                            lastDate: DateTime(DateTime.now().year + 1),
                            context: context
                          );
                        },
                        validator: (value) {
                          if ((value.toString().isEmpty) || (DateTime.tryParse(value.toString()) == null)) {
                            return 'Please enter a valid date';
                          } 
                          return null;
                        },
                        onChanged: (value) {
                          setState(() =&gt; expense.date = value);
                        },
                      )
                    ),
  </code></pre>