I have a schedule in data files, one per class, and want to present them multiple ways.
- list by course, and days available
- days of week with courses and times.
the first is easy because i just iterate over the files, and print the structure of my individual data files
{
"name": "Hawt Dog Yoga",
"description": "We just eat michigans.",
"days": [
{
"when": "Tuesday 12:00 am",
"instructor": "eddie"
},
{
"when": "Wednesday 12:00 am",
"instructor": "eddie"
}
]
}
but because my when
field is only the time (format: ‘dddd h:mm a’), and not a full date or timestamp, nothing in hugo seems capable of parsing/sorting it correctly. I use netlify CMS to enter/manipulate the dates, I think it uses moment.js to parse dates in this format fine.
<!-- rebuild data as distinct maps for each day with a when:class strcuture -->
{{ $scratch := newScratch }}
{{- range $class := $.Site.Data.classes -}}
{{ range $day := $class.days }}
{{ $scratch.SetInMap (index (split $day.when " ") 0) (printf "%s %s" (index (split $day.when " ") 1) (index (split $day.when " ") 2) ) ($class) }}
{{ end }}
{{ end }}
<!-- iterate over days of week, printing all classes -->
{{- range $day := (slice "Monday" "Tuesday" "Wednesday" ) -}}
<div><strong>{{$day}}</strong> -
<!-- this works, but order is not intelligent time, just numerical (so 9am shows after 12pm) -->
{{ $scratch.GetSortedMapValues $day }}
</div>
{{- end -}}
{{- range $day := (slice "Monday" "Tuesday" "Wednesday" ) -}}
<div><strong>{{$day}}</strong> -
{{ range $time, $class := $scratch.Get $day }}
<!-- dateFormat and time both barf on partial time field -->
{{ time $time }}
{{ end }}
</div>
{{ end }}
Any advice? Is there a way to tell dateformat to use the same ‘dddd h:mm a’ that works in moment.js and core golang? https://gobyexample.com/time-formatting-parsing