readableDateTime function

String readableDateTime(
  1. String isoDateString
)

Implementation

String readableDateTime(String isoDateString) {
  DateTime dateTime = DateTime.parse(isoDateString);
  dateTime = DateTime(
      dateTime.year, dateTime.month, dateTime.day); // Hanya pertahankan tanggal

  DateTime now = DateTime.now();
  now = DateTime(now.year, now.month, now.day); // Hanya pertahankan tanggal

  Duration difference = now.difference(dateTime);

  if (difference.inDays == 0) {
    return "Hari ini";
  } else if (difference.inDays == 1) {
    return "Kemarin";
  } else if (difference.inDays <= 7) {
    return "${difference.inDays} hari yang lalu";
  } else if (difference.inDays <= 30) {
    int weeks = (difference.inDays / 7).round();
    return "$weeks minggu yang lalu";
  } else if (difference.inDays <= 365) {
    int months = (difference.inDays / 30).round();
    return "$months bulan yang lalu";
  } else {
    int years = (difference.inDays / 365).round();
    return "$years tahun yang lalu";
  }
}