timeBetween method Null safety

String timeBetween(
  1. DateTime from,
  2. DateTime to
)

Method computing time between two DateTime objects

Implementation

String timeBetween(DateTime from, DateTime to) {
  final nbSecondsFrom = from.second;
  final nbSecondsTo = to.second;
  int diffSeconds = nbSecondsTo - nbSecondsFrom;

  if (diffSeconds < 60)
    return ("Now");
  else if (diffSeconds >= 60 && diffSeconds < 3600)
    return ((diffSeconds / 60).toString() + " min");
  else if (diffSeconds >= 3600 && diffSeconds < 86400)
    return ((diffSeconds / 3600).toString() + " h");
  else if (diffSeconds >= 86400 && diffSeconds < 604800)
    return ((diffSeconds / 86400).toString() + " d");
  else if (diffSeconds >= 604800 && diffSeconds < 7257600)
    return ((diffSeconds / 604800).toString() + " mois");
  return ((diffSeconds / 7257600).toString() + " ans");
}