splitDateFormat static method

List<String> splitDateFormat(
  1. String? dateFormat, {
  2. DateTimePickerMode? mode,
})

Split date format to array.

Implementation

static List<String> splitDateFormat(String? dateFormat,
    {DateTimePickerMode? mode}) {
  if (dateFormat == null || dateFormat.length == 0) {
    return [];
  }
  List<String> result = dateFormat.split(RegExp(DATE_FORMAT_SEPARATOR));
  if (mode == DateTimePickerMode.datetime) {
    // datetime mode need join day format
    List<String> temp = [];
    StringBuffer dayFormat = StringBuffer();
    for (int i = 0; i < result.length; i++) {
      String format = result[i];
      if (isDayFormat(format)) {
        // find format pre-separator
        int end = dateFormat.indexOf(format);
        if (end > 0) {
          int start = 0;
          if (i > 0) {
            start = dateFormat.indexOf(result[i - 1]) + result[i - 1].length;
          }
          dayFormat.write(dateFormat.substring(start, end));
        }
        dayFormat.write(format);
      } else if (isTimeFormat(format)) {
        temp.add(format);
      }
    }
    if (dayFormat.length > 0) {
      temp.insert(0, dayFormat.toString());
    } else {
      // add default date format
      temp.insert(0, DATETIME_PICKER_DATE_FORMAT);
    }
    result = temp;
  }
  return result;
}