How would I get the exact time like the roblox output bar?
Hour:Minute:Second.Milisecond
How would I get the exact time like the roblox output bar?
Hour:Minute:Second.Milisecond
Depends, do you want to get the time in a specific time-zone? Time since the server started? Time since you started printing?
You can use os.date("%X")
to get hour:minute:second. If you want the milliseconds you are going to need to do something else though as the os library for roblox only returns integers Except for os.clock(), but that probably won’t help you.. Here is a link to the os library.
local Date = DateTime.now():ToLocalTime() -- Local Time
local timestamp = `{Date.Hour}:{Date.Minute}:{Date.Second}:{Date.Millisecond}`
-- Timestamp
That Simple, it does not require os.date
like what @dragonvale5555 said.
This can work if you want your current clock time, but if you want ms to hr:min:sec.milli, you can just throw the millisecond value into this function I had made a while ago.
function MillisecondsToHourMinSec(TimeInput) -- ex of output: 00:19:12.242
if typeof(TimeInput) ~= 'number' then
warn("You didn't input a number!")
return nil
end
local Hours = 0
local Minutes = 0
local Seconds = 0
Hours = math.floor(TimeInput/3600000)
Minutes = math.floor(TimeInput/60000) - (Hours*60)
Seconds = TimeInput/1000 - (Minutes*60) - (Hours*3600)
if Hours <= 9 then
Hours = "0"..Hours
end
if Minutes <= 9 then
Minutes = "0"..Minutes
end
if Seconds < 10 then
Seconds = "0"..Seconds
end
return Hours..":"..Minutes..":"..Seconds
end
Not gonna lie, I forgot I made this and this post reminded me that I had this laying around.
Edit: Fixed a small bug
Testing it gave me innaccurate results.
print(MillisecondsToHourMinSec(os.time())) --> 466:10:50.85700000007637
along with other things.
Edit: I think I missread.
os.time()
gives you unix time in SECONDS not milliseconds. Also you can just use math.round()
on the ms value if you really want to
you can just use tick()
for that, which doesnt work either.
You’d have to do tick() *1000
to get it in milliseconds, although it would still just round to the closest second from that anyway, you’d have to feed it an exact millisecond value if you want anything accurate.
Edit: Didn’t know that tick gave a more specific value than seconds, but you still need to do tick() * 1000
to get it in ms
Nope.
print(MillisecondsToHourMinSec(math.floor(tick() * 1e3)/1e3))
print(MillisecondsToHourMinSec(tick() * 1e3))
Still gives me inaccurate values
print(MillisecondsToHourMinSec(math.floor(tick() * 1e3)/1e3)
All this does is just give you the same thing as if you just input tick()
print(MillisecondsToHourMinSec(tick() * 1e3))
This one should be accurate though
os.clock() give the most precise time-related results because it comes directly from you device’s processor. You could use formatting combined with subtracted os.clock()
As @AborayStudios mentioned, use os.clock()
for the most accurate results, since it returns processor time. Unlike os.time()
, os.date()
, tick()
or DateTime.now()
(this is a thing), clock returns precise time in miliseconds.
You will have to combine both to achieve that milisecond effect, here’s some sample code:
os.date("%H:%M:%S." .. string.sub(string.match(tostring(os.clock()), "%.(%d+)"), 1, 3)
This is a string format, wrap this in a print statement.
Results however don’t seem to be exact. os.clock()
don’t seem to match Studio’s miliseconds, but this works anyway.
os.clock() is inconsistent when it comes to studio vs in-game. I get the results I want in studio but not in game. Have any of you found a solution to this problem yet?
Running os.clock() on Studio returns time since Studio started, and running it ingame returns seconds since the client started. Simple.
my goal is to see the amount of time that it takes for the remote to call to the client. I send the os.clock() in the remote and fire the client then in the client I take the current os.clock() and subtract it with what was sent. It works in studio giving me the time but in the client it gives me over 5 digits of numbers I don’t understand.
While testing in Studio, the server and client are hosted in your own system, hence why no difference.
In-game however, I don’t think I’ll need to explain myself.
Okay so what’s the problem here then? What can I do to fix this. Here is the code:
-- Server
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
while wait(5) do
game.ReplicatedStorage.Remote:FireClient(player, os.clock())
end
end)
end)
-- Client
game.ReplicatedStorage.Remote.OnClientEvent:Connect(function(sendTime)
local deltaTime = os.clock() - sendTime
print(deltaTime)
end)
You can’t really “fix” it since it’s not a problem. Unfortunately I’m not into networking so I can’t really provide an answer to that.
So, you want to get the server’s time from the server’s timezone? Why are you sending it to the client through a remote event? Just use a number value and rely on replication.