Help with time formatting

I’m trying to make a function that converts an amount of seconds (for example, 124681) into a formatted version. I’ve made the day and hour successfully but I am unsure on how to make the minute and second ones.

I’m trying to go for something like this:
totalseconds = 124681
d_format = 1
h_format = 10
m_format = ???
s_format = ???

local d_format = (math.floor(seconds / 86400));  
local h_format = (math.floor(seconds / 3600) - (d_format * 24));
local m_format = ??
local s_format = ??

Just keep going like you were. I’d also recommend doing the subtraction to the seconds just to make things clearer:

local function SecToDMHS(seconds)
  local days = math.floor(seconds / 86400);
  seconds -=  days * 86400
  local hours = math.floor(seconds / 3600);
  seconds -= hours * 3600
  local minutes = math.floor(seconds / 60);
  seconds -= minutes * 60
  return days, hours, minutes, seconds
end

Edit: this is wrong gimme a second lol actually seems right

1 Like
function formatSeconds(seconds)
  local days = math.floor(seconds / 86400)
  local hours = math.floor((seconds % 86400) / 3600)
  local minutes = math.floor((seconds % 3600) / 60)
  local remainingSeconds = seconds % 60

  local formattedTime = string.format("%d:%02d:%02d:%02d", days, hours, minutes, remainingSeconds)

  return formattedTime
end

Here’s an explanation of the code:

First, we define a variable named Prize and set its value to the location of the prize in the game world. This will be used later to calculate the distance between the player and the prize.

Next, we define a function called UpdateLengthBar that takes one argument, player. This function will be called every frame to update the length bar for the specified player.

Inside the UpdateLengthBar function, we calculate the distance between the player and the prize using the magnitude function of the Vector3 class. The magnitude function calculates the distance between two points in 3D space.

We then calculate the maximum distance between the player and the prize. This will be used later to determine how much of the length bar should be filled.

We create a variable named length and set its value to the distance between the player and the prize divided by the maximum distance.

Next, we create a UI element called a Frame and set its size and position on the screen. We also set its color and transparency to make it look like a length bar.

We then create another UI element called an ImageLabel and set its position on the screen based on the position of the Frame. We also set its image to the player’s logo icon and its size to a fraction of the Frame’s size, depending on the value of length.

Finally, we parent the ImageLabel to the Frame so that it moves along with the length bar.

We call the UpdateLengthBar function for each player in the game using a for loop.

Overall, this script uses the distance between the player and the prize to create a length bar that displays the player’s logo icon and changes length based on the distance. This can be a helpful visual cue for players to see how close they are to the prize.

This function takes an input of seconds and returns a formatted string that includes days, hours, minutes, and seconds. Here’s how the function works:

days is calculated by dividing the total number of seconds by the number of seconds in a day (86400) and rounding down.
hours is calculated by taking the remainder of the total number of seconds divided by the number of seconds in a day (86400) and dividing that by the number of seconds in an hour (3600), then rounding down.
minutes is calculated by taking the remainder of the total number of seconds divided by the number of seconds in an hour (3600) and dividing that by the number of seconds in a minute (60), then rounding down.
remainingSeconds is calculated by taking the remainder of the total number of seconds divided by the number of seconds in a minute (60).
The string.format function is used to format the values of days, hours, minutes, and remainingSeconds into a string with the format “d:hh:mm:ss”. The “%d” format specifier is used for days, which is an integer, and the “%02d” format specifier is used for hours, minutes, and remainingSeconds, which are integers that should be displayed with two digits, padded with zeros if necessary.
You can use this function to format the totalseconds value like this:

local totalseconds = 124681
local formattedTime = formatSeconds(totalseconds)
print(formattedTime) -- Output: "1:10:46:21"

This will print the formatted time string “1:10:46:21”, which represents 1 day, 10 hours, 46 minutes, and 21 seconds.

1 Like