NetCDF in QGIS: use expression to set time range of bands

In QGIS, a NetCDF file is opened as a raster with one band per timestamp. Unfortunately, the time is not set in the Temporal Settings, but it is shown in the name of the band and can be converted with an expression

When I open a NetCDF file in QGIS as a raster, I get a raster layer with a lot of bands, one band per timestamp. Unfortunately, the temporal settings of these bands are not set when the file is opened, so I cannot simply select individual timestamps with the temporal controller or play an animation. At least, the timestamp is in the band name, but in the format time=1867128 (hours since 1800-01-01). Fortunately, this is sufficient to enter the start and end in the Layer Properties under “Temporal” using an expression (Configuration: “Fixed Time Range Per Band”).

For Begin:

-- extract epoch and convert it to datetime
to_datetime(
  right(
    regexp_substr(@band_name, 'hours since \\d{4}-\\d{2}-\\d{2}')
  , 10)
) 
-- Add the number of hours
+
make_interval(
  hours:=to_int(
	regexp_substr(regexp_substr(@band_name, 'time=\\d+'), '\\d+')
)) 

Now you can read from the table how long the time steps are (e.g. 6 hours). For “End”, you can add the corresponding time interval to the above expression, like so:

-- Above expression
+ make_interval(hours:=6)