Skip to content

Instantly share code, notes, and snippets.

@13protons
Last active December 2, 2017 19:41
Show Gist options
  • Save 13protons/72c6d22d95f3ed80a511 to your computer and use it in GitHub Desktop.
Save 13protons/72c6d22d95f3ed80a511 to your computer and use it in GitHub Desktop.
Localize dates in Jekyll template without specifying timezone offset
# A tiny Jekyll filter for giving custom dates a proper timezone offset
# example usage `{{ post.startDate | tz_local | date: '%l:%M %P'}}`
# Why? If you omit a timezone offset, Jekyll automatically adds +0000...which is probably not what you want.
# This let's dates like `startDate: 2015-1-14 18:00:00` render correcty in local time. DST is calculated correctly
# NOTE: This will still inherit Jekyll's timezone property as set in _config.yml
module TZLocal
def tz_local(input)
input = input + (Time.parse(input.to_s).gmt_offset) * -1
end
end
Liquid::Template.register_filter(TZLocal)
@EmilStenstrom
Copy link

Thank you for this snippet. Should really be part of Jekyll in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment