Home > Net >  Show time difference between 2 date
Show time difference between 2 date

Time:06-10

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,
        ),
      ),

CodePudding user response:

You could utilize the Example result on DartPad

CodePudding user response:

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 '';
    }
  }

CodePudding user response:

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.

CodePudding user response:

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}');
  • Related