Home > Net >  Dart function no returning the expected value
Dart function no returning the expected value

Time:09-17

I have created a function that receives a String date and returns a string that shows the time between the given date and the current DateTime in the format: x days ago, x months ago, just now, x years ago, yesterday, etc:

This is the code for that function:

 String tiempoDesdeFecha(String dateString, {bool numericDates = true}) {
      DateTime date = DateTime.parse(dateString);
      final date2 = DateTime.now();
      final difference = date2.difference(date);

      if ((difference.inDays / 365).floor() >= 2) {
        return "hace".tr() '${(difference.inDays / 365).floor()}' "yearsago".tr();
      } else if ((difference.inDays / 365).floor() >= 1  ) {
        return (numericDates) ? '1yearago'.tr() : 'lastyear'.tr() " " (numericDates).toString();
      } else if ((difference.inDays / 30).floor() >= 2) {
        return "hace".tr() '${(difference.inDays / 365).floor()}' "monthsago".tr();
      } else if ((difference.inDays / 30).floor() >= 1) {
        return (numericDates) ? '1monthago'.tr() : 'lastmonth'.tr();
      } else if ((difference.inDays / 7).floor() >= 2) {
        return "hace".tr() '${(difference.inDays / 7).floor()}'  'weeksago'.tr();
      } else if ((difference.inDays / 7).floor() >= 1) {
        return (numericDates) ? "1weekago".tr() : 'lastweek'.tr();
      } else if (difference.inDays >= 2) {
        return "hace".tr() '${difference.inDays}'  'daysago'.tr();
      } else if (difference.inDays >= 1) {
        return (numericDates) ? '1dayago'.tr() : 'yesterday'.tr();
      } else if (difference.inHours >= 2) {
        return "hace".tr() '${difference.inHours}'  'hoursago'.tr();
      } else if (difference.inHours >= 1) {
        return (numericDates) ? '1hourago'.tr() : 'anhourago'.tr();
      } else if (difference.inMinutes >= 2) {
        return "hace".tr() '${difference.inMinutes} '  'minutesago'.tr();
      } else if (difference.inMinutes >= 1) {
        return (numericDates) ? '1minuteago'.tr() : 'aminuteago'.tr();
      } else if (difference.inSeconds >= 3) {
        return "hace".tr() '${difference.inSeconds}'  'secondsago'.tr();
      } else {
        return 'justnow'.tr();
      }
    }

I have included the code to show the returned string in one of the four Locale I am using.

Everything is working fine, but the condition:

else if ((difference.inDays / 30).floor() >= 2) {
        return "hace".tr() '${(difference.inDays / 365).floor()}' "monthsago".tr();

which is always returning 0 months ago.

I am calling the function from:

fecha_recibida = DateFormat('yyyy-MM-dd HH:mm',"en").format(date);
fecha_recibida = tiempoDesdeFecha(fecha_recibida);

I am testing that issue with following String date: "2021-5-5 19:34"

What am I doing wrong?

CodePudding user response:

I believe the error is in the returned string,

difference.inDays / 365 must be difference.inDays / 30.

So the condition should look like..

else if ((difference.inDays / 30).floor() >= 2) {
        return "hace".tr() '${(difference.inDays / 30).floor()}' "monthsago".tr();

A plus, as @aman mentioned, you could use timeago package

  • Related