Hello! I have a Date system in table. How do I find the most recent one?
The numbers are arranged as follows:
year, day of month, month, hours, minutes, seconds
Hello! I have a Date system in table. How do I find the most recent one?
The numbers are arranged as follows:
year, day of month, month, hours, minutes, seconds
Unfortunately I can’t think of a way, as I’m not that good at tables
Logically if ur storing in order, last one should be the latest
get latest by checking each number and getting the highest one (likely best method)
Are those DateTime
objects in the table or actual strings?
You can create a for loop (an ordering algorithm) that basically goes through the entire table, and compares the previous entry with the next one.
Something like this:
local dateTable = {} -- The table that you store the date objects in, assuming it's full here.
local recentDate = dateTable[1] -- The variable we use to store the most recent date object, and to compare with other date objects.
-- We set it to the first member of the dateTable to allow comparing with other members of the table.
for _, dateObject in dateTable do -- A loop that goes through the entire dateTable table.
if recentDate < dateObject then -- This condition can be modified as it's here just for an example, you can modify this to make sure the dateObject is more recent than the recentDate.
recentDate = dateObject -- dateObject is more recent than the recentDate, so we update the recentDate variable to the dateObject.
end
end
print(recentDate) -- This should print the most recent date!
You would have to do a linear search and compare the UnixTimestamp
or UnixTimestampMillis
properties, depending on your use cases.
local function GetMostRecentDateFromTable(Table: {DateTime}): DateTime?
local ResultDate = nil
local ResultTimestamp = 0
for Index, Date in Table do
local Timestamp = Date.UnixTimestamp
if Timestamp > ResultTimestamp then
ResultDate = Date
end
end
return ResultDate
end
Note that this function makes the assumption that you don’t have negative timestamps. If you do, this can be fixed by changing the initial value to -math.huge
.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.