HUGO

Sorting by custom *time* field (not date)

I have a schedule in data files, one per class, and want to present them multiple ways.

  1. list by course, and days available
  2. 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

I’m going to suggest a (maybe) low-effort solution here: can you configure Netlify CMS to give a date picker, and simply assign the “days” array by picking a proper date/time on a particular week of some year? This way you can store an actual datetime in the front matter, and just format it to remove the month/year.

Thanks @setphen.

I am doing exactly as you suggest with a date picker, but moment.js stores it in the format I mention. Round trip saving/displaying works fine in netlify, shows as current week.

I thought about including arbitrary week & year in the storage format, but was hoping there was some way to get at the native capabilities of go that supports this kind of date parsing so the underlying data files could remain “true” to their purpose.

It;s a possible last resort though…

I ended up modifying my data, but rather than add abritrary year, etc, I broke the data down into distinct day and time fields. I format the times as hh:mm a/p. And then I can modify presentation however i need.

Every class foo.json gets an array of decomposed date objects

"name":"foo",
  "days": [
    {
      "day": "Thursday",
      "start": "05:15 pm",
      "end": "06:00 pm"
    }
  ],