lib/parseformat.js
function replaceEndian (options, matchedPart, first, separator, second, third) {
  var parts
  var hasSingleDigit = Math.min(first.length, second.length, third.length) === 1
  var hasQuadDigit = Math.max(first.length, second.length, third.length) === 4
  var preferredOrder = typeof options.preferredOrder === 'string' ? options.preferredOrder : options.preferredOrder[separator]

  first = parseInt(first, 10)
  second = parseInt(second, 10)
  third = parseInt(third, 10)
  parts = [first, second, third]
  preferredOrder = preferredOrder.toUpperCase()

  // If first is a year, order will always be Year-Month-Day
  if (first > 31) {
    parts[0] = hasQuadDigit ? 'YYYY' : 'YY'
    parts[1] = hasSingleDigit ? 'M' : 'MM'
    parts[2] = hasSingleDigit ? 'D' : 'DD'
    return parts.join(separator)
  }

  // Second will never be the year. And if it is a day,
  // the order will always be Month-Day-Year
  if (second > 12) {
    parts[0] = hasSingleDigit ? 'M' : 'MM'
    parts[1] = hasSingleDigit ? 'D' : 'DD'
    parts[2] = hasQuadDigit ? 'YYYY' : 'YY'
    return parts.join(separator)
  }

  // if third is a year ...
  if (third > 31) {
    parts[2] = hasQuadDigit ? 'YYYY' : 'YY'

    // ... try to find day in first and second.
    // If found, the remaining part is the month.
    if (preferredOrder[0] === 'M' && first < 13) {
      parts[0] = hasSingleDigit ? 'M' : 'MM'
      parts[1] = hasSingleDigit ? 'D' : 'DD'
      return parts.join(separator)
    }
    parts[0] = hasSingleDigit ? 'D' : 'DD'
    parts[1] = hasSingleDigit ? 'M' : 'MM'
    return parts.join(separator)
  }

  // if we had no luck until here, we use the preferred order
  parts[preferredOrder.indexOf('D')] = hasSingleDigit ? 'D' : 'DD'
  parts[preferredOrder.indexOf('M')] = hasSingleDigit ? 'M' : 'MM'
  parts[preferredOrder.indexOf('Y')] = hasQuadDigit ? 'YYYY' : 'YY'

  return parts.join(separator)
}
function replaceEndian (options, matchedPart, first, separator, second, third) {
  var parts
  var hasSingleDigit = Math.min(first.length, second.length, third.length) === 1
  var hasQuadDigit = Math.max(first.length, second.length, third.length) === 4
  var preferredOrder = typeof options.preferredOrder === 'string' ? options.preferredOrder : options.preferredOrder[separator]

  first = parseInt(first, 10)
  second = parseInt(second, 10)
  third = parseInt(third, 10)
  parts = [first, second, third]
  preferredOrder = preferredOrder.toUpperCase()

  // If first is a year, order will always be Year-Month-Day
  if (first > 31) {
    parts[0] = hasQuadDigit ? 'YYYY' : 'YY'
    parts[1] = hasSingleDigit ? 'M' : 'MM'
    parts[2] = hasSingleDigit ? 'D' : 'DD'
    return parts.join(separator)
  }

  // Second will never be the year. And if it is a day,
  // the order will always be Month-Day-Year
  if (second > 12) {
    parts[0] = hasSingleDigit ? 'M' : 'MM'
    parts[1] = hasSingleDigit ? 'D' : 'DD'
    parts[2] = hasQuadDigit ? 'YYYY' : 'YY'
    return parts.join(separator)
  }

  // if third is a year ...
  if (third > 31) {
    parts[2] = hasQuadDigit ? 'YYYY' : 'YY'

    // ... try to find day in first and second.
    // If found, the remaining part is the month.
    if (preferredOrder[0] === 'M' && first < 13) {
      parts[0] = hasSingleDigit ? 'M' : 'MM'
      parts[1] = hasSingleDigit ? 'D' : 'DD'
      return parts.join(separator)
    }
    parts[0] = hasSingleDigit ? 'D' : 'DD'
    parts[1] = hasSingleDigit ? 'M' : 'MM'
    return parts.join(separator)
  }

  // if we had no luck until here, we use the preferred order
  parts[preferredOrder.indexOf('D')] = hasSingleDigit ? 'D' : 'DD'
  parts[preferredOrder.indexOf('M')] = hasSingleDigit ? 'M' : 'MM'
  parts[preferredOrder.indexOf('Y')] = hasQuadDigit ? 'YYYY' : 'YY'

  return parts.join(separator)
}
function replaceEndian ( options , matchedPart , first , separator , second , third ) { var parts var hasSingleDigit = Math . min ( first . length , second . length , third . length ) === 1 var hasQuadDigit = Math . max ( first . length , second . length , third . length ) === 4 var preferredOrder = typeof options . preferredOrder === 'string' ? options . preferredOrder : options . preferredOrder [ separator ] first = parseInt ( first , 10 ) second = parseInt ( second , 10 ) third = parseInt ( third , 10 ) parts = [ first , second , third ] preferredOrder = preferredOrder . toUpperCase ( ) // If first is a year, order will always be Year-Month-Day if ( first > 31 ) { parts [ 0 ] = hasQuadDigit ? 'YYYY' : 'YY' parts [ 1 ] = hasSingleDigit ? 'M' : 'MM' parts [ 2 ] = hasSingleDigit ? 'D' : 'DD' return parts . join ( separator ) } // Second will never be the year. And if it is a day, // the order will always be Month-Day-Year if ( second > 12 ) { parts [ 0 ] = hasSingleDigit ? 'M' : 'MM' parts [ 1 ] = hasSingleDigit ? 'D' : 'DD' parts [ 2 ] = hasQuadDigit ? 'YYYY' : 'YY' return parts . join ( separator ) } // if third is a year ... if ( third > 31 ) { parts [ 2 ] = hasQuadDigit ? 'YYYY' : 'YY' // ... try to find day in first and second. // If found, the remaining part is the month. if ( preferredOrder [ 0 ] === 'M' && first < 13 ) { parts [ 0 ] = hasSingleDigit ? 'M' : 'MM' parts [ 1 ] = hasSingleDigit ? 'D' : 'DD' return parts . join ( separator ) } parts [ 0 ] = hasSingleDigit ? 'D' : 'DD' parts [ 1 ] = hasSingleDigit ? 'M' : 'MM' return parts . join ( separator ) } // if we had no luck until here, we use the preferred order parts [ preferredOrder . indexOf ( 'D' ) ] = hasSingleDigit ? 'D' : 'DD' parts [ preferredOrder . indexOf ( 'M' ) ] = hasSingleDigit ? 'M' : 'MM' parts [ preferredOrder . indexOf ( 'Y' ) ] = hasQuadDigit ? 'YYYY' : 'YY' return parts . join ( separator ) }
if we can't find an endian based on the separator, but there still is a short date with day, month & year, we try to make a smart decision to identify the order
if we can t find an endian based on the separator but there still is a short date with day month & year we try to make a smart decision to identify the order