-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Make DateTime.parse() more permissive, accept single digit parts. #36067
Comments
@lrhn ? |
The input to We could loosen those requirements, perhaps by allowing single-digit month and day or <4 digit year, but I'd prefer not to. Since we allow omitting Creating a string from integers, and then immediately parsing it, is not required, or even the best way, to create a var reconstructedTime = DateTime(testDateTime.year, testDateTime.month, testDateTime.day, testDateTime.hour, testDateTime.minute); I'd even recommend creating the If you want to format a date/time in a particular way, you can use (That |
The reason I filed it as an issue is simply because if you use DateTime.(something).toString( ) to pull something out of DateTime and then try to put it back together by using the parse, it causes an error. Intuitively, since DateTime.parse( ) takes a string, and you got strings out of the DateTime, one expects that reassembling them using parse( ) to be successful. It might help if this simply had a different error message. Instead of: |
Bump on this. |
Still no plan to change the parse behavior. The |
I looked at doing a PR on this but there seem to be multiple places it could be addressed and I'm not sure which would be appropriate. So, here it is...
SDK Version:
I'm not sure which version of Dart is actually being used because Flutter is utilizing its own copy of date_time.dart located at flutter\bin\cache\pkg\sky_engine\lib\core , which I believe can, at times, be a version behind the Dart SDK. My Flutter version is 1.2.1 and I'm on Beta.
Problem:
DateTime.parse() fails with an Invalid date format error if you hand it a string that is only one character (it doesn't have leading zeros in the month, day, hour and minute for any value 9 and under).
Compounding Problem:
toString() or string interpolation drops leading zeros. IE:
print(selectedDateTime); // Returns 2019-04-18 06:03:00.000
print(selectedDateTime.hour.toString()); // Returns 6, not 06
Running this code will recreate the issue:
A hacky workaround is to use a pad( ), but it would be much better if this were handled in the framework.
Example:
${_datePicked.month.toString().padLeft(2, "0")}
Thanks,
Scott Stoll
The text was updated successfully, but these errors were encountered: