mikefarah/yq · error

could not find %v for format_time

Error message

could not find %v for format_time

What it means

`getStringParameter` evaluates the expression that should supply a string parameter (layout, format, or timezone) for the datetime operators. If evaluating the argument expression yields zero matching nodes, it cannot extract a string and returns this error. The message always says 'format_time' regardless of which operator called it (a known message quirk).

Source

Thrown at pkg/yqlib/operator_datetime.go:17

package yqlib

import (
	"container/list"
	"errors"
	"fmt"
	"strconv"
	"time"
)

func getStringParameter(parameterName string, d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (string, error) {
	result, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode)

	if err != nil {
		return "", err
	} else if result.MatchingNodes.Len() == 0 {
		return "", fmt.Errorf("could not find %v for format_time", parameterName)
	}

	return result.MatchingNodes.Front().Value.(*CandidateNode).Value, nil
}

func withDateTimeFormat(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
	if expressionNode.RHS.Operation.OperationType == blockOpType || expressionNode.RHS.Operation.OperationType == unionOpType {
		layout, err := getStringParameter("layout", d, context, expressionNode.RHS.LHS)
		if err != nil {
			return Context{}, fmt.Errorf("could not get date time format: %w", err)
		}
		context.SetDateTimeLayout(layout)
		return d.GetMatchingNodes(context, expressionNode.RHS.RHS)

	}
	return Context{}, errors.New(`must provide a date time format string and an expression, e.g. with_dtf("Monday, 02-Jan-06 at 3:04PM MST"; <exp>)`)

}

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Check that the argument expression actually resolves: run it standalone (`yq '.format' file.yml`) and ensure it prints a value
  2. Provide a literal string instead of an expression: `with_dtf("2006-01-02"; .)` or `format_time("Monday, 02 Jan 2006")`
  3. If the value may be missing, guard with a default that is a concrete string: `format_time(.format // "2006-01-02")`
  4. Fix typos in variable references (`$fmt` vs `$format`) — undefined variables yield empty node sets

Example fix

// before: .fmt missing in input -> empty result
yq 'with_dtf(.fmt; .date | format_time("2006-01-02"))' file.yml
// after: concrete or defaulted layout
yq 'with_dtf(.fmt // "2006-01-02"; .date | format_time("2006-01-02"))' file.yml
Defensive patterns

Strategy: validation

Validate before calling

# verify the argument resolves to a value before running the operator
yq '(.format // "2006-01-02") | type' file.yml   # must print !!str
# or test the inner expression standalone first

Try / catch

// wrap yq run; on this error, retry with a literal default layout
if out, err := run("yq", expr, file); err != nil &&
	strings.Contains(out, "could not find") {
	out, err = run("yq", withLiteralLayout(expr), file)
}

Prevention

When it happens

Trigger: Calling `with_dtf(<empty>; ...)`, `format_time(<empty>)`, or `tz(<empty>)` where the argument expression selects nothing — e.g. a missing key like `.missing_key`, a `select()` that matches no nodes, or `// ""`-style fallbacks that produce empty results, or passing `empty` as the argument.

Common situations: Typo'd variable/field name inside the format argument (`$fmt` undefined, `.format` missing from the input), conditional defaults that evaluate to empty, or building expressions programmatically where the RHS slot is empty.

Related errors


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/2c815175c15c090a. Report an issue: GitHub.