dateAsString function Null safety

String dateAsString(
  1. DateTime? date,
  2. {bool withoutSeconds = false,
  3. String separator = '-'}
)

Returns a standard representation of date: YYYY.mm.dd-HH:MM:SS date date to convert. If null the current date and time is taken [withoutSeconds: true: the seconds are not part of the result separator: the string between date and time, default: '-'

Implementation

String dateAsString(DateTime? date,
    {bool withoutSeconds = false, String separator = '-'}) {
  date ??= DateTime.now();
  var rc = StringBuffer();
  rc.write(date.year);
  rc.write('.');
  rc.write(sprintf('%02d', [date.month]));
  rc.write('.');
  rc.write(sprintf('%02d', [date.day]));
  rc.write(separator);
  rc.write(sprintf('%02d', [date.hour]));
  rc.write(':');
  rc.write(sprintf('%02d', [date.minute]));
  if (!withoutSeconds) {
    rc.write(':');
    rc.write(sprintf('%02d', [date.second]));
  }
  return rc.toString();
}