datify 1.2.0 copy "datify: ^1.2.0" to clipboard
datify: ^1.2.0 copied to clipboard

An extensible library providing the functionality of parsing strings to extract dates in different formats.

example/datify_example.dart

import 'package:datify/datify.dart';

void main() {
  const inputs = [
    '31.12.2021',
    '2022-02-23T10:00:00Z',
    '20th of January, 2021',
    'Sept 5, 2020',
    '14 лютого 2022',
    '3 січ 26',
    '12/31/2021',
    'The meeting on 15 March 2022 at 10:30',
    '20 of January', // incomplete
    '31.02.2021', // does not exist
    'Maybe tomorrow', // not a date
  ];

  for (final input in inputs) {
    print('$input → ${describe(Datify.parse(input))}');
  }

  // read ambiguous dates in the US order
  DatifyConfig.dayFirst = false;
  print('05/06/2021 → ${describe(Datify.parse('05/06/2021'))}');
  DatifyConfig.dayFirst = true;

  // add month names in another language
  DatifyConfig.addNewMonthsLocale([
    'janvier', 'février', 'mars', 'avril', 'mai', 'juin', //
    'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre',
  ]);
  print('14 juillet 2021 → ${describe(Datify.parse('14 juillet 2021'))}');
}

String describe(Datify datify) {
  final date = datify.date;
  if (date != null) {
    return date.toIso8601String().substring(0, 10);
  }

  if (datify.isComplete) {
    return 'not an existing date';
  }

  final parts = {
    'year': datify.year,
    'month': datify.month,
    'day': datify.day,
  }.entries.where((part) => part.value != null);

  return parts.isEmpty
      ? 'no date found'
      : parts.map((part) => '${part.key} ${part.value}').join(', ');
}
9
likes
160
points
677
downloads

Documentation

API reference

Publisher

verified publishermitryp.com.ua

Weekly Downloads

An extensible library providing the functionality of parsing strings to extract dates in different formats.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

More

Packages that depend on datify