# Testing for parseable date string

**URL:** <https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806>\
**Category:** support\
**Created:** [August 27, 2025, 8:38pm UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806 "2025-08-27T20:38:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmcltr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tmcltr/32/1170_2.png) [@tmcltr](https://discourse.gohugo.io/u/tmcltr)\
**Post date:** [August 27, 2025, 8:38pm UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806/1 "2025-08-27T20:38:03Z")

</div>

I have a yaml file containing metadata used around the site. One particular value _usually_ contains a nicely formatted date string (`YYYY-MM-DD`), but in some cases it might contain pure text, like a fuzzy future date (`early 2026`, for example).

These dates are displayed in a couple of different formats, and Hugo is very helpful with that.

The problem I’m running into is that there seems to be no way for me to test whether a given string is parseable as a date. I’d like to do something like this:

`{{ if (“early 2026” | time) }}`  
` it worked`  
`{{ else }}`  
` it did not work`  
`{{ end }}`

But any attempt that direction throws an error:

`execute of template failed at <time>: error calling time: unable to parse date: early 2026`

One dumb workaround I thought of is to turn either the parseable date strings or the fuzzy strings into slices so that I can use `reflect.IsSlice` and handle them that way.

Is that my best option? Or is there a better way?

---

<div class="post-metadata">

**Author:** ![tmcltr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tmcltr/32/1170_2.png) [@tmcltr](https://discourse.gohugo.io/u/tmcltr)\
**Post date:** [August 27, 2025, 9:00pm UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806/2 "2025-08-27T21:00:24Z")

</div>

I just thought of an even dumber workaround: test if the string begins with a number, and if so, treat it as a date.

I don’t like it, but it will probably work for now.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 27, 2025, 9:40pm UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806/3 "2025-08-27T21:40:27Z")

</div>

Use the [`try`](https://gohugo.io/functions/go-template/try/) statement, perhaps within a function:

layouts/\_partials/functions/is-date.html

```plaintext
{{ $result := false }}
{{ with try (time.AsTime .) }}
  {{ with .Value }}
    {{ $result = true }}
  {{ end }}
{{ end }}
{{ return $result }}

```

Then call the function:

```plaintext
{{ $d := slice "2042-06-07" "early 2042" "07 Jun 2042" }}
{{ range $d }}
  {{ if partialCached "functions/is-date.html" . . }}
    {{ . | time.Format ":date_short" }}
  {{ else }}
    {{ warnf "%q is not a parsable date" . }}
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![tmcltr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tmcltr/32/1170_2.png) [@tmcltr](https://discourse.gohugo.io/u/tmcltr)\
**Post date:** [August 28, 2025, 4:22am UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806/4 "2025-08-28T04:22:23Z")

</div>

That’s the answer! That’s pretty much exactly what I was looking for. I should have thought to search the docs for “try”!

Thanks very much for the help.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [August 30, 2025, 4:22am UTC](https://discourse.gohugo.io/t/testing-for-parseable-date-string/55806/5 "2025-08-30T04:22:33Z")

</div>

Closed on August 30, 2025, 4:22am UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
