parseToFormat method

dynamic parseToFormat (String parseDate, [ String format ])

Implementation

parseToFormat(String parseDate, [String format]) {
  var parse = DateTime.parse(parseDate);
  var jParse = gregorianToJalali(parse.year, parse.month, parse.day);
  if (format == null) format = _defualtVal;

  String newFormat = format;

  print(parse.weekday);

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