Skip to content

Instantly share code, notes, and snippets.

@vsajip
Forked from zwh8800/datetime.lua
Created October 21, 2025 03:55
Show Gist options
  • Select an option

  • Save vsajip/6b87e2563368ee12f88b46bf1d509d29 to your computer and use it in GitHub Desktop.

Select an option

Save vsajip/6b87e2563368ee12f88b46bf1d509d29 to your computer and use it in GitHub Desktop.
lua ISO 8601 datetime parser - https://repl.it/IQuI/5
function parse_json_date(json_date)
local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-])(%d?%d?)%:?(%d?%d?)"
local year, month, day, hour, minute,
seconds, offsetsign, offsethour, offsetmin = json_date:match(pattern)
local timestamp = os.time{year = year, month = month,
day = day, hour = hour, min = minute, sec = seconds}
local offset = 0
if offsetsign ~= 'Z' then
offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
if xoffset == "-" then offset = offset * -1 end
end
return timestamp + offset
end
print(string.format('%d', parse_json_date('2017-05-25T06:21:43Z')))
print(string.format('%d', parse_json_date('2017-05-25T06:21:43+0830')))
print(string.format('%d', parse_json_date('2017-05-25T06:21:43-0400')))
print(string.format('%d', parse_json_date('2017-05-25T06:21:43.213Z')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment