How do I check date/clock time roblox?

How do I check if it’s a specific time, like if it’s 6:00 PM, because I want to write a code where if a certain date/ time reached then trigger something, e.g. if it’s 14th July 2022 6:00PM then print (“It’s 6pm”)

And another question, is the time global meaning it will activate regardless of people’s timezone?

2 Likes

Hope this snippet helps

print(os.date("%A, %B %d, %X", os.time()))
--print current date and time, in format of weekday, month day, full time

For more information, check the Roblox documentation on os.

If you’re on about the time of day in a server then you can get it using Lighting:GetMinutesAfterMidnight()
Here is an example:

local Lighting = game:GetService("Lighting")
function CheckTime()
   if Lighting:GetMinutesAfterMidnight == 1080 then
      print("It's 6 PM")
   end
end
Lighting.Changed:Connect(CheckTime)

Thanks, what would the universal timezone be? Instead of it being based on a client’s timezone?

From what I saw on the documentary on os, it already records it under UTC time.

Oh ok, so if a USA player joins a server and a EU player joins a different server, the os.time would be the same for both of the servers? or how does it work for the UTC to be maintained

Reason why i’m asking is because, i want to trigger something when a certain time is reached, at the same time on all servers that have the script. So if the next hour clock occurs let’s say it prints something.

So if it’s 5:43 PM, but then next hour clock, i.e 6:00PM occurs, then it prints something, please can you show me how i can do this?

UTC (Universal Coordinated Time) is always the same, no matter what.
You can try this for a triggered event:

local plrtime = tonumber(os.date("%H", os.time()))
local seconds = tonumber(os.date("%S", os.time()))
local ampm = os.date("%p", os.time())
local debounce = false
while wait() do
    plrtime = tonumber(os.date("%H", os.time()))
    seconds = tonumber(os.date("%S", os.time()))
    ampm = os.date("%p", os.time())
    if plrtime == 6 and ampm == "PM" and seconds == 0 and debounce == false then
        print("It's 6:00!")
        debounce = true
        --do other code, if necessary
    else
        debounce = false
    end
end
1 Like

Thank you very much, i really do appreciate your help, I humbly ask, if you do not mind please can you show me how I can also get the day and month and year? I would like to also apply those in the checks i.e. if day == “12th” and month == “July” or 06, etc

Simple, just add extra checks to the if statement:

local plrtime = tonumber(os.date("%H", os.time()))
local seconds = tonumber(os.date("%S", os.time()))
local extratime = os.date("%B %d", os.time())
local ampm = os.date("%p", os.time())
local debounce = false
while wait() do
    plrtime = tonumber(os.date("%H", os.time()))
    seconds = tonumber(os.date("%S", os.time()))
    ampm = os.date("%p", os.time())
    if plrtime == 6 and ampm == "PM" and seconds == 0 and debounce == false and extratime == "July 12" then
        print("It's 6:00!")
        debounce = true
        --do other code, if necessary
    else
        debounce = false
    end
end

Thank you very much, is the time in the example you gave me, using UTC and not a player’s local time? just to ensure

Yes, I’ll quote to Roblox’s documentation:
“This library currently serves the purpose of providing information about the system time under the UTC format.”

1 Like

Thank you, so if a USA player joins a server and a EU player joins a server, since the while checker is using UTC, it will both trigger at same times regardless of their differing (EU/USA) timezones?

Just wondering because in studio it shows this

01:20 which was my time 1 second ago,

but when i google UTC time
image

it shows 00:20, how comes studio prints my timezone and not the UTC?

In army time, 00:20 equals 12:20, so you might need to add adjustments.
I just went off of the documentation.
Either the documentation has false information, or there’s something I didn’t get.

However, one solution could be to run the script in server, then use that information for all clients.