formatTimestamp function

String formatTimestamp({
  1. required int? timestamp,
  2. bool showTime = true,
  3. bool showDate = true,
})

Formats a timestamp

Implementation

String formatTimestamp({
  required int? timestamp,
  bool showTime = true,
  bool showDate = true,
}) {
  if (timestamp != null) {
    /// Format timestamp
    final DateTime formattedTimestamp =
        DateTime.fromMillisecondsSinceEpoch(timestamp);
    final String formattedDate =
        DateFormat('d MMM, yy').format(formattedTimestamp);
    final String formattedTime = DateFormat('jm').format(formattedTimestamp);

    return '${showDate ? formattedDate : ''} ${showTime ? 'at $formattedTime' : ''}';
  }

  return '';
}