Jump to content

timestamp


Sendy

Recommended Posts

If you're asking for time remaining until a specific target timestamp, then

local target = --[[ some timestamp ]]
local current = getRealTime().timestamp
local remaining_time = target - current

local hours = math.floor((remaining_time % 86400) / 3600)
local mins = math.floor((remaining_time % 3600) / 60)
local secs = math.floor(remaining_time % 60)

 

  • Thanks 1
Link to comment
local target = os.time() + 86399
local current = os.time()
local remaining_time = target - current

local hours = math.floor((remaining_time % 86400) / 3600)
local mins = math.floor((remaining_time % 3600) / 60)
local secs = math.floor(remaining_time % 60)

local msg = string.format("%02d:%02d:%02d", hours, mins, secs)

You can also format the code from the solution above using this method with string.format.

result: 23:59:59

  • Thanks 1
Link to comment

Thanks when i want add years, months, days?
 

local years = math.floor((remaining_time % number) / number)
local months = math.floor((remaining_time % number) / number)
local days = math.floor((remaining_time % number) / number)
local hours = math.floor((remaining_time % 86400) / 3600)
local mins = math.floor((remaining_time % 3600) / 60)
local secs = math.floor(remaining_time % 60)

 

Edited by Sendy
Link to comment

Months have variable length so I wouldn't advise using that. If you need to, use the average month length which is actually slightly >30 days, meaning 2592000 seconds (more more accurately, 30.44 days = 2630016 seconds) in a month.

local years = math.floor(remaining_time / 31557600)
local months = math.floor((remaining_time % 31557600) / 2592000)
local days = math.floor((remaining_time % 2592000) / 86400)
local hours = math.floor((remaining_time % 86400) / 3600)
local mins = math.floor((remaining_time % 3600) / 60)
local secs = math.floor(remaining_time % 60)

 

Edited by Investor
  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...