Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
3.1 kB
2
Indexable
Never

  String getVerboseDateTimeRep(
    String date,
    bool isArabic,
  ) {
    if (date == '') {
      return isArabic ? 'الان' : 'Just Now';
    }
    var dateUTC = DateTime.parse(date.replaceAll('/', '-')).toUtc();

    var dateTime = dateUTC.toLocal();

    DateTime now = DateTime.now().toUtc();

    if (now.day == dateTime.day &&
        now.month == dateTime.month &&
        now.year == dateTime.year &&
        now.difference(dateTime).inMinutes < 2) {
      return isArabic ? 'الان' : 'Just Now';
    }

    if (now.day == dateTime.day &&
        now.month == dateTime.month &&
        now.year == dateTime.year &&
        now.difference(dateTime).inMinutes >= 2 &&
        now.difference(dateTime).inMinutes < 60) {
      int diff = now.difference(dateTime).inMinutes;

      return TranslationService().isLocaleArabic()
          ? 'قبل $diff دقيقة'
          : '$diff minutes ago';
    }

    if (now.day == dateTime.day &&
        now.month == dateTime.month &&
        now.year == dateTime.year &&
        now.difference(dateTime).inHours >= 1 &&
        now.difference(dateTime).inHours < 2) {
      int diff = now.difference(dateTime).inHours;

      return isArabic ? 'قبل ساعة' : '$diff hour ago';
    }

    if (now.difference(dateTime).inHours >= 2 &&
        now.difference(dateTime).inHours < 3) {
      int diff = now.difference(dateTime).inHours;

      return isArabic ? 'قبل ساعتين' : '$diff hours ago';
    }

    if (now.difference(dateTime).inHours >= 3 &&
        now.difference(dateTime).inHours <= 10) {
      int diff = now.difference(dateTime).inHours;

      return isArabic ? 'قبل $diff ساعات' : '$diff hours ago';
    }

    if (now.difference(dateTime).inHours > 10 &&
        now.difference(dateTime).inHours <= 24) {
      return isArabic ? 'اليوم' : 'Today';
    }

    if (now.difference(dateTime).inHours > 24 &&
        now.difference(dateTime).inHours <= 48) {
      return isArabic ? 'أمس' : 'Yesterday';
    }

    if (now.difference(dateTime).inDays >= 2 &&
        now.difference(dateTime).inDays <= 6) {
      int diff = now.difference(dateTime).inDays;

      return isArabic ? 'قبل $diff أيام' : '$diff days ago';
    }

    if (now.difference(dateTime).inDays >= 7 &&
        now.difference(dateTime).inDays < 30) {
      int diff = now.difference(dateTime).inDays ~/ 7;

      return isArabic ? 'قبل $diff أسبوع' : '$diff weeks ago';
    }

    if (now.difference(dateTime).inDays > 29 &&
        (now.difference(dateTime).inDays ~/ 30).toInt() < 12) {
      double diff = now.difference(dateTime).inDays / 30;

      int months = diff.toInt();

      return isArabic ? '$months شهر' : '$months month';
    }

    if ((now.difference(dateTime).inDays ~/ 30).toInt() >= 12) {
      double diff = now.difference(dateTime).inDays / 365;

      int years = diff.toInt();

      return isArabic ? '$years س' : '$years y';
    } else {
      return '${intl.DateFormat.yMMMMd().format(dateTime)} - ${intl.DateFormat.jm().format(dateTime)}';
    }
  }