gohugoio/hugo · error

invalid arguments supplied to `time`

Error message

invalid arguments supplied to `time`

What it means

The `time` identifier in Hugo templates is both a namespace and a function. The dispatcher in tpl/time/init.go returns the namespace with 0 args, calls AsTime with 1 arg (value) or 2 args (value, location), and rejects 3 or more arguments with this error since no overload exists for that arity.

Source

Thrown at tpl/time/init.go:55

			Context: func(cctx context.Context, args ...any) (any, error) {
				// Handle overlapping "time" namespace and func.
				//
				// If no args are passed to `time`, assume namespace usage and
				// return namespace context.
				//
				// If args are passed, call AsTime().

				switch len(args) {
				case 0:
					return ctx, nil
				case 1:
					return ctx.AsTime(args[0])
				case 2:
					return ctx.AsTime(args[0], args[1])

				// 3 or more arguments. Currently not supported.
				default:
					return nil, errors.New("invalid arguments supplied to `time`")
				}
			},
		}

		ns.AddMethodMapping(ctx.AsTime,
			nil,
			[][2]string{
				{`{{ (time "2015-01-21").Year }}`, `2015`},
			},
		)

		ns.AddMethodMapping(ctx.Duration,
			[]string{"duration"},
			[][2]string{
				{`{{ mul 60 60 | duration "second" }}`, `1h0m0s`},
			},
		)

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Call `time` with one argument (the value) or two (value, timezone): `{{ time "2024-01-01" "America/New_York" }}`.
  2. If you meant to format, convert first then format: `{{ (time .Date).Format "2006-01-02" }}` or use `time.Format`.
  3. Check pipelines — a piped value counts as the last argument, so remove redundant explicit arguments.

Example fix

<!-- before -->
{{ time "2024-01-01" "UTC" "2006-01-02" }}
<!-- after -->
{{ (time "2024-01-01" "UTC").Format "2006-01-02" }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $d := .Params.date }}
{{ if $d }}{{ $t := time.AsTime $d }}{{ end }}

Type guard

{{ if or (reflect.IsMap $d) (reflect.IsSlice $d) }}{{ warnf "time needs a string or time.Time, got collection" }}{{ else }}{{ time.AsTime $d }}{{ end }}

Try / catch

{{ with try (time.AsTime .Params.eventDate) }}{{ if .Err }}{{ errorf "invalid date in %s: %s" $.File.Path .Err }}{{ else }}{{ .Value.Format "2006-01-02" }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: Calling `{{ time "2024-01-01" "UTC" "extra" }}` or otherwise piping/passing three or more arguments to `time` — e.g. `{{ time .Date .Site.Params.tz .Something }}` or a mistaken pipe that appends an extra final argument.

Common situations: Confusing `time` with `time.Format` or `dateFormat` and passing a layout string as a third argument; pipeline misuse where `|` appends the piped value as an extra argument (`{{ "2024-01-01" | time "UTC" "x" }}`); porting templates from other engines with different date-function signatures.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/a280921765345288.json. Report an issue: GitHub ↗.