1

I'm trying to show time difference between 2 date in my flutter app. How can I do so with my formatted date ? And I also need to show it as a string.

For example: 6 Jun 2022 14 Jun 2022 (8 Days)

Here's my current code:

      Text(
        'Created: ' +
            DateFormat('d MMM y').format(
              DateTime.parse(
                ticketData['date_created']
                    .toDate()
                    .toString(),
              ),
            ),
        style: primaryColor400Style.copyWith(
          fontSize: fontSize13,
        ),
      ),
      TextSpan(
        text: DateFormat('d MMM y').format(
          DateTime.parse(
            ticketData['due_date']
                .toDate()
                .toString(),
          ),
        ),
        style: weight400Style.copyWith(
          fontSize: fontSize14,
          color: hintColor,
        ),
      ),
1

4 Answers 4

1

You could utilize the difference() method from the DateTime class. Here some example:

void main() {
  final String createdDateInput = '2022-06-03T01:37:02+0000';
  final String dueDateInput = '2022-06-10T01:37:02+0000';
  
  final DateTime createdDateTime = DateTime.parse(createdDateInput);
  final DateTime dueDateTime = DateTime.parse (dueDateInput);
  
  final String createdDate = DateFormat('d MMM y').format(createdDateTime);
  final String dueDate = DateFormat('d MMM y').format(dueDateTime);
  final Duration duration = dueDateTime.difference(createdDateTime);
  
  print('$createdDate - $dueDate (${duration.inDays} day(s))');
}

Example result on DartPad

0

Try this:

static String timeAgoSinceDate(String dateString,
      {bool numericDates = true}) {
    try {
      DateTime notificationDate = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
          .parse(dateString, true)
          .toUtc()
          .toLocal();
      final date2 = DateTime.now();
      final difference = date2.difference(notificationDate);
      if (difference.inDays > 8) {
        return '${notificationDate.year}/${notificationDate.month}/${notificationDate.day}';
      } else if ((difference.inDays / 7).floor() >= 1) {
        return (numericDates) ? '1 week ago' : 'Last week';
      } else if (difference.inDays >= 2) {
        return '${difference.inDays} days ago';
      } else if (difference.inDays >= 1) {
        return (numericDates) ? '1 day ago' : 'Yesterday';
      } else if (difference.inHours >= 2) {
        return '${difference.inHours} hours ago';
      } else if (difference.inHours >= 1) {
        return (numericDates) ? '1 hour ago' : 'An hour ago';
      } else if (difference.inMinutes >= 2) {
        return '${difference.inMinutes} minutes ago';
      } else if (difference.inMinutes >= 1) {
        return (numericDates) ? '1 minute ago' : 'A minute ago';
      } else if (difference.inSeconds >= 3) {
        return '${difference.inSeconds} seconds ago';
      } else {
        return 'Just now';
      }
    } catch (e) {
      return '';
    }
  }
0

You can also try this,

final birthdayDate = DateTime(1967, 10, 12);
final toDayDate = DateTime.now();
var different = toDayDate.difference(birthdayDate).inDays;

print(different);

The difference is measured in seconds and fractions of seconds. The difference above counts the number of fractional seconds between midnight at the beginning of those dates. If the dates above had been in local time, not UTC, then the difference between two midnights may not be a multiple of 24 hours due to daylight saving differences.

0

here is another way:

final DateTime startDate = DateTime.parse('2020-11-11');
final DateTime endDate = DateTime.parse('2022-11-11');

final int startDateInMS = startDate.millisecondsSinceEpoch;
final int endDateInMS = endDate.millisecondsSinceEpoch;

final Duration duration = Duration(milliseconds: endDateInMS - startDateInMS);

print('days: ${duration.inDays}');
print('Hours: ${duration.inHours}');
print('Minuts: ${duration.inMinutes}');
print('Seconds: ${duration.inSeconds}');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.