Has anyone gotten any meaningful use out of datetime.tryparse? I'm trying to accept date formats such as...
MM/dd/yyyy
MM-dd-yyyy
MM.dd.yyyy
it seems DateTime.tryParse
always returns null for all of these formats. Is there a library or a more convenient way to accept date times of different formats.
To check if a date is between 2 other ones in Flutter and Dart, you can use the isBefore() and isAfter() methods of the DateTime class.
isDate(String str) - check if the string is a date. isAfter(String str [, date]) - check if the string is a date that's after the specified date (defaults to now). isBefore(String str [, date]) - check if the string is a date that's before the specified date.
DateFormat is used to convert / parse dates into specific format (ex : yyyy-MM-d, yy-MM-dd). In order to use this class, we need to add intl as a dependency in pubspec. yaml and then import the package in the dart file.
DateTime.[try]parse
only parses a very distinct format, namely:
a subset of ISO 8601 which includes the subset accepted by RFC 3339
To parse formats like 06/09/2019
use the DateFormat
class from package:intl
.
DateFormat.yMd().parse('06/09/2019'); // defaults to en_US, i.e. MM/dd/yyyy
This code:
import 'package:intl/intl.dart';
main() {
print(DateFormat.yMd().parse('06/09/2019'));
}
prints
2019-06-09 00:00:00.000
as expected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With