parseToFormat method

String parseToFormat(
  1. String parseDate, [
  2. String? format
])

Implementation

String parseToFormat(String parseDate, [String? format]) {
  final parse = DateTime.parse(parseDate);
  final List<int> solarDate =
      gregorianToSolar(parse.year, parse.month, parse.day);

  var newFormat = format ?? _defaultFormat;

  if (newFormat.contains(yyyy)) {
    newFormat = newFormat.replaceFirst(yyyy, _digits(solarDate[0], 4));
  }
  if (newFormat.contains(yy)) {
    newFormat = newFormat.replaceFirst(yy, _digits(solarDate[0] % 100, 2));
  }
  if (newFormat.contains(mm)) {
    newFormat = newFormat.replaceFirst(mm, _digits(solarDate[1], 2));
  }
  if (newFormat.contains(m)) {
    newFormat = newFormat.replaceFirst(m, solarDate[1].toString());
  }
  if (newFormat.contains(MM)) {
    newFormat = newFormat.replaceFirst(MM, monthLong[solarDate[1] - 1]);
  }
  if (newFormat.contains(M)) {
    newFormat = newFormat.replaceFirst(M, monthShort[solarDate[1] - 1]);
  }
  if (newFormat.contains(dd)) {
    newFormat = newFormat.replaceFirst(dd, solarDate[2].toString());
  }
  if (newFormat.contains(d)) {
    newFormat = newFormat.replaceFirst(d, _digits(solarDate[2], 2));
  }
  if (newFormat.contains(w)) {
    newFormat =
        newFormat.replaceFirst(w, ((solarDate[2] + 7) ~/ 7).toString());
  }
  if (newFormat.contains(DD)) {
    newFormat = newFormat.replaceFirst(DD, dayLong[parse.weekday - 1]);
  }
  if (newFormat.contains(D)) {
    newFormat = newFormat.replaceFirst(D, dayShort[parse.weekday - 1]);
  }
  if (newFormat.contains(HH)) {
    newFormat = newFormat.replaceFirst(HH, _digits(parse.hour, 2));
  }
  if (newFormat.contains(H)) {
    newFormat = newFormat.replaceFirst(H, parse.hour.toString());
  }
  if (newFormat.contains(hh)) {
    newFormat = newFormat.replaceFirst(hh, _digits(parse.hour % 12, 2));
  }
  if (newFormat.contains(h)) {
    newFormat = newFormat.replaceFirst(h, (parse.hour % 12).toString());
  }
  if (newFormat.contains(AM)) {
    newFormat = newFormat.replaceFirst(
        AM, parse.hour < 12 ? 'قبل از ظهر' : 'بعد از ظهر');
  }
  if (newFormat.contains(am)) {
    newFormat = newFormat.replaceFirst(am, parse.hour < 12 ? 'ق.ظ' : 'ب.ظ');
  }
  if (newFormat.contains(nn)) {
    newFormat = newFormat.replaceFirst(nn, _digits(parse.minute, 2));
  }
  if (newFormat.contains(n)) {
    newFormat = newFormat.replaceFirst(n, parse.minute.toString());
  }
  if (newFormat.contains(ss)) {
    newFormat = newFormat.replaceFirst(ss, _digits(parse.second, 2));
  }
  if (newFormat.contains(s)) {
    newFormat = newFormat.replaceFirst(s, parse.second.toString());
  }
  if (newFormat.contains(SSS)) {
    newFormat = newFormat.replaceFirst(SSS, _digits(parse.millisecond, 3));
  }
  if (newFormat.contains(S)) {
    newFormat = newFormat.replaceFirst(S, parse.millisecond.toString());
  }
  if (newFormat.contains(uuu)) {
    newFormat = newFormat.replaceFirst(uuu, _digits(parse.microsecond, 2));
  }
  if (newFormat.contains(u)) {
    newFormat = newFormat.replaceFirst(u, parse.microsecond.toString());
  }
  return newFormat;
}