How to get the current IRL time?

So currently at GMT+1 it’s 19:44 PM and I have a webhook that logs when a moderator joined, the script works but I want it to also say “Moderator cmodii logged on XX:XX AM/PM”
Here is the script:


local modlogger = "url"
local http = game:GetService("HttpService")

-- For when moderator joins:
game.Players.PlayerAdded:Connect(function(plr)

if plr.Name == "cmodii" then
    local data = {
        ["embeds"] = {
            {
                ["title"] = "**Moderator Logged on**", -- what are we logging
                ["description"] = "Moderator "..plr.Name.." has logged on." -- i want the time to be here such as Moderator cmodii has logged on 19:45 PM GMT+1
            }
        }
    }
    local finaldata = http:JSONEncode(data)
    http:PostAsync(modlogger, finaldata)
end
end)

Thanks in advance!

9 Likes

Take a look at this and if u still got other questions for this than dont ask me. https://developer.roblox.com/en-us/api-reference/lua-docs/os

2 Likes

As the person above stated. Use the os to code your IRL time script. Should work since it’s a simple task.

Using os.time

local TIME_ZONE = 1

local date = os.date("!*t")
local hour = (date.hour + TIME_ZONE) % 24
local ampm = hour < 12 and "AM" or "PM"
local timestamp = string.format("%02i:%02i %s", ((hour - 1) % 12) + 1, date.min, ampm)

print(timestamp)
11 Likes

Thank you, however I found out how to do it, still this is better for timezone and AM/PM checking, my solution was this:

local hours = os.date("*t")["hour"] -- for hour irl
local mins = os.date("*t")["min"] -- for min irl
5 Likes

I added the timezone check because calling os.time with !*t as the parameter uses GMT, while *t makes it use the local time on the machine that runs the code. It might be better to define the timestamp in the script since in an online game the server code will be running on Roblox’s servers, causing it to return their local time, which may be different from yours.

2 Likes

Ah alright got it, thanks man appreciate it!

You can also implement a timestamp (add new index called timestamp in embed dictionary) if you’d like for precise time that looks just like the time a Discord message has (e.g. Today at 9:01 AM) using this if you want:

Seems like this became the top result for this topic when googled, just wanted to update this with refurbished code that uses mod 24 instead of 12

local TIME_ZONE = 1

local date = os.date("!*t")
local hour = (date.hour + TIME_ZONE) % 24
local timestamp = string.format("%02i:%02i", hour, date.min)

print(timestamp)
11 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.