os.date("%X", 0) returns 02:00:00

Hi there!
Im trying to convert time from seconds to H:M:S
When i do it, everytimes i got 02:00:00
It’s adds 2 hours without like a start of counter, is it must be this or im wrong somewhere?

print(os.date("%X", 0)) -- 02:00:00

P.S I know how to convert from seconds to H:M:S using function, but is it real to do using os.date()

The second argument should be the time

print(os.date("%X", os.time()))
2 Likes

What I get is that you’re trying to make a countdown timer? If so, then I recommend you don’t use os.date() for this, and instead just format the time remaining, heres an example:

local TimeLeft = 2 * 60 * 60 -- 2 Hours (2 Hours * 60 Minutes * 60 Seconds)

function FormatToHMS(seconds)
  -- Kinda hard to explain this math --
  local Hours = seconds / 3600
  local Minutes = (seconds / 60) % 60
  local Seconds = seconds % 60
  return string.format("%02i:%02i:%02i", Hours, Minutes, Seconds)
end

while TimeLeft > 0 do
  -- While the timer is still on and there isn't 0 time left --
  TimeLeft = TimeLeft - 1
  print("TIME LEFT", FormatToHMS(TimeLeft))
  wait(1)
end

So basically, the FormatToHMS function takes a value in Seconds, and formats it to look like a Hours:Minutes:Seconds String.

You can play around with the while TimeLeft > 0 do loop, and make it fit to your needs, but this should work (haven’t tested)

Also, side note, please research the forums before making a support topic, this question has been asked many times (heres an example) so just make sure to check!

1 Like

Thank you, but im using this way at the moment, just wanted to ask why does it returns 2 hours like a basic always.

That’s because of your timezone os is in UTC time os.time() returns the current UTC time.

My timezone is UTC so it prints 00:00:00 for me.