Test for existence of a time portion of a date?

I have some frontmatter that lists some datetime stuff for events, like this:

eventdate: 2023-05-01

I’ve been turning this string into a datetime object with time function so I can specify .Format on it.

However, in my templates I’m wondering if there’s a simple way to test for the existence of a time portion of the eventdate string? If there’s no ...T18:00:00 hanging out after that string I’d like to skip including the time if possible in my template. Any ideas?

Thank you!

{{ $t := time.AsTime .Params.eventDate }}

<p>Date = {{ $t | time.Format ":date_medium" }}</p>
{{ if or $t.Hour $t.Minute $t.Second }}
 <p>Time = {{ $t | time.Format ":time_medium" }}</p>
{{ end }}

Dood. I think you’ve almost single-handedly answered every darn question I’ve ever had and I just want to thank you (I learn a ton reading your responses). :pray:

Thank you for this!

You’re welcome. But if you really want to be precise…

{{ if or $t.Hour $t.Minute $t.Second $t.Nanosecond }}

That would cover this case as well:

eventDate = "2023-03-02T00:00:00.12345-08:00"
1 Like

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