|Returns the current in-game time.|
|Returns||number the current in-game time|
|Prints current time in 12 hour format (with AM and PM).|
local time = os.time() local formattedTime = textutils.formatTime(time, false) print("The time is " .. formattedTime)
- You should use textutils.formatTime() if you want to print the time in readable form.
Using the returned value
os.time() will return a value between 0.000 and 23.999. This corresponds to the day-night cycle of the server. Midnight is at 0.000 (when the cycle resets), while midday is at 12.000. 6.000 is the morning, when the player will wake up if they slept in a bed. The player can first get into bed for the night at 18.541.
Each full day/night cycle corresponds to 20 minutes (1,200 seconds) in real-life, or 24 'hours' on the os.time scale. Each 'hour' on the os.time scale is 50 seconds in real-life. After the decimal place, tenths represent 5 seconds, hundredths represent 0.5 seconds, and thousandths represent 0.05 seconds (one tick).
|Minecraft ticks||Real-time Equivalent||Elapsed time||os.time()|
Minecraft ticks can be used in commands to set server time. For example, you can set the time to midnight by typing in . The real-time equivalent shows what the time would look like if it were in real-life. Elapsed time indicates how many seconds would have elapsed from the point that your character gets of bed in the morning.
To convert os.time() to real-life seconds since the in-game midnight, you can multiply it by 50. Example:
local secondsSinceMidnight = os.time() * 50
To convert os.time() to minecraft ticks, multiply by 1,000, add 18,000, and then use a modulo (%) to ensure the value is within 0 and 23999.
local MCTicks = (os.time() * 1000 + 18000)%24000