GardenMobile & interfaces

Reading an older Dart date-formatting helper

Formatting examples and the calendar assumptions hidden in today, yesterday, week, and elapsed-time checks.

A display format, an interchange timestamp, and a calendar comparison serve different purposes. The original helper collected all three around DateTime and intl:

import 'package:intl/intl.dart';
// const String dateString = '10/12/2020';
// final DateTime dateTimeObj = DateFormat('dd/MM/yyyy').parse(dateString);
// print('dateTimeObj $dateTimeObj');
//  
// final String dateTimeFormatString = DateFormat('yyyy-MM-dd').format(dateTimeObj);
// print('dateTimeFormatString $dateTimeFormatString');

// Today 11:08 PM
// => 'Today ${DateFormat('h:mm a').format(toDay)}'
//
// Yesterday 11:08 PM
// => 'Yesterday ${DateFormat('h:mm a').format(yesterDay)}'
//
// Tuesday 11:08 PM
// => DateFormat('EEEE h:mm a').format(weekD2)
//
// Wed, Jul 29, 11:08 PM
// => DateFormat('E, MMM dd, h:mm a').format(yearDay)
//
// Jul 25, 2019, 11:08 PM
// => DateFormat('MMM dd, yyyy, h:mm a').format(otherDay)

/// Extension for DateTime
extension DateTimeExtension on DateTime {
  // Convert DateTime to String
  // 2020-04-03T11:57:00
  String toDateTimeString() {
    return DateFormat('yyyy-MM-ddThh:mm:ss').format(this);
  }

  // Check this is now
  bool isToday(DateTime now) {
    final DateTime today = DateTime(now.year, now.month, now.day);
    final DateTime dayCheck = DateTime(year, month, day);
    return today == dayCheck;
  }

  // Check this is yesterday
  bool isYesterday(DateTime now) {
    final DateTime yesterday = DateTime(now.year, now.month, now.day - 1);
    final DateTime dayCheck = DateTime(year, month, day);
    return yesterday == dayCheck;
  }

  // Check if this is in current week
  bool isWeekday(DateTime now) {
    final DateTime firstDayOfTheWeek =
        now.subtract(Duration(days: now.weekday));
    return isAfter(firstDayOfTheWeek);
  }

  /// 1+ hours difference between current time and the old one.
  bool isOver1Hour(DateTime prevTime) {
    return !prevTime.add(const Duration(hours: 1)).isAfter(this);
  }
}

/// Extension for DateTime from String
extension DateTimeStringExtendsion on String {
  // Parse DateTime from String
  // 2020-04-03T11:57:00
  DateTime toDateTime() {
    return DateFormat('=yyyy-MM-ddThh:mm:ss').parse(this);
  }
}

Check the format literally

The parse pattern includes a leading = that the formatter does not emit. It also uses hh, a twelve-hour field, and carries no timezone offset. The resulting text should not be assumed to be a complete ISO timestamp or a reliable round trip.

Check the calendar boundary

The today/yesterday methods reconstruct calendar dates in their available time context. The method named isWeekday only checks a lower time bound derived from the supplied date; it has no upper bound and is not a complete current-week test.

The one-hour comparison is an elapsed-time check, a different question again. These historical methods are preserved for inspection and were not executed or corrected into a new utility for this revision.

← Back to the garden