Search code examples
jekyll

How can you convert Time to UTC in Jekyll?


I have the following {{ site.data.wedding.Ceremony.Start | date: "%Y%m%dT%H:%M:%S%:z" }}, which currently outputs: 20200101T16:00:00+02:00

I want to convert that time to UTC, regardless of the timezone set in the site.data.wedding.Ceremony.Start.

Contents of wedding.json:

{
    "ShortName": "Bride&Groom",
    "Bride": "Bride",
    "Groom": "Groom",
    "Ceremony": {
        "Start": "2020-01-01T16:00:00+02:00",
        "End": "2020-01-01T18:00:00+02:00"
    },
    "Reception": {
        "Start": "2020-01-01T18:30:00+02:00",
        "End": "2020-01-02T02:00:00+02:00"
    }
}

Solution

  • Currently, there is no Liquid filter to convert a Date to UTC. However, unless you're building your site via GitHub Pages, you can use a plugin to define the filter.

    Simply save the following code into _plugins/utc_filter.rb:

    module Jekyll
      module UTCFilter
        def to_utc(date)
          time(date).utc
        end
      end
    end
    
    Liquid::Template.register_filter(Jekyll::UTCFilter)
    

    Then use the above filter in your template:

    {{ site.data.wedding.Ceremony.Start | to_utc | date: "%Y%m%dT%H:%M:%S%:z" }}
    

    You can simply add additional methods to the module above for defining more filters.