如何在 Jekyll 中将时间转换为 UTC?

How can you convert Time to UTC in Jekyll?

我有以下 {{ site.data.wedding.Ceremony.Start | date: "%Y%m%dT%H:%M:%S%:z" }},当前输出:20200101T16:00:00+02:00

我想将该时间转换为 UTC,而不考虑 site.data.wedding.Ceremony.Start 中设置的时区。

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"
    }
}

目前,没有将日期转换为 UTC 的 Liquid 过滤器。 但是,除非您通过 GitHub 页面构建站点,否则您可以使用插件来定义过滤器。

只需将以下代码存入_plugins/utc_filter.rb:

module Jekyll
  module UTCFilter
    def to_utc(date)
      time(date).utc
    end
  end
end

Liquid::Template.register_filter(Jekyll::UTCFilter)

然后在您的模板中使用上述过滤器:

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

您可以简单地向上面的模块添加额外的方法来定义更多过滤器。