How to get the discord webhook ISO8601 timestamp?

I am looking to use a timestamp for a Discord webhook and want to get the exact time as the current time of the script running to send a timestamp. I already know how to create an embed and JSONEncode the data to be sent, just need the timestamp.


How would I get the ISO8601 timestamp? I’m not even sure what an ISO8601 is in the first place. Closest I could find was this:

Even then, I have no idea how this even works.

ISO 8601 is basically a way to format dates. Since the whole thing is so complex, you won’t be able to do it with just one string match, you’ll need multiple functions which handle different parts of converting it.

You should see this StackOverflow post about converting the date/time to ISO 8601.

Would something like this work?

local function parseDateTime()
	local osDate = os.date("!*t")
	local Y,M,D = osDate["year"], osDate["month"], osDate["day"]
	local h,m,s = osDate["hour"], osDate["min"], osDate["sec"]
	return os.time({year=Y, month=M, day=D, hour=(h), min=(m), sec=s})
end

I don’t think so. That’s just returning a number.

Try this:

local function format(num, digits)
	return string.format("%0" .. digits .. "i", num)
end

local function parseDateTime()
	local osDate = os.date("!*t")
	local year, mon, day = osDate["year"], osDate["month"], osDate["day"]
	local hour, min, sec = osDate["hour"], osDate["min"], osDate["sec"]
	return year .. "-" .. format(mon, 2) .. "-" .. format(day, 2) .. "T" .. format(hour, 2) .. ":" .. format(min, 2) .. ":" .. format(sec, 2) .. "Z"
end

Basically, just stick the year, month, day, hour, minute, and second together according to the format’s specification. The format function just makes sure it zero-pads it to two digits. (e.g. 2 -> 02)

Edit: Here’s the reverse, for anyone curious. It just uses a string pattern and string.match. (iso8601 -> os.date table):

local function parseIso8601(date)
	local year, month, day, hour, min, sec = date:match("^(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z")
	return {
		year = year,
		month = month,
		day = day,
		hour = hour,
		min = min,
		sec = sec,
	}
end
4 Likes

Thank you a lot!

image

EDIT: Figured out what the parseDateTime() formatting does.

I’d be interested to hear about how the formatting works for parseIso8601 local variable formatting ("^(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z") except the Z letter since the ISO8601 timestamp needs it.

1 Like