Timestamp script Broke after 2020

I am having A problem with my script that gets the current time but I noticed after 2020 there was an error in this time script.

function getcurrectMonth(DoTy, months)
	local var2 = DoTy
	for i = 1, #months do
		if var2 <= months[i] then return i, var2 else var2 = var2 - months[i] end
	end
end

function timestamp()
	local months = {31, 28, 31, 30, 31, 30, 31,31,30, 31, 30, 31}
	local timezone, offset_hours = "GMT", 0
	local secondspassed = (script.Parent.Parent.FavoriteTimeValue.Value)+(3600*offset_hours)
	local yearspassed = math.floor(secondspassed/31557600)
	local YoTc = yearspassed+1970 -- Year of the Century (change the 30 to 130 when it's 2100 :D (if it's 2100....))
	local DoTy =  math.floor((secondspassed-(yearspassed*(31557600)))/86400)+1
	if YoTc/4-math.floor(YoTc/4) == 0 then months[2] = 29 end
	local MoTy, DoTm = getcurrectMonth(DoTy, months)
	local HR = math.floor((secondspassed-((math.floor(secondspassed/86400))*86400))/3600)
	local MIN = math.floor((secondspassed-((math.floor(secondspassed/3600))*3600))/60)
	local SEC = math.floor((secondspassed-((math.floor(secondspassed/60))*60)))
	if string.len(HR)==1 then HR = "0"..HR end
	if string.len(MIN)==1 then MIN = "0"..MIN end
	if string.len(SEC)==1 then SEC = "0"..SEC end
	return MoTy.."/"..DoTm.."/"..YoTc.." "..HR..":"..MIN..":"..SEC
end

This is the Issue that I’m having in this script
image

I tried to Print all the values and the Months and Days said “nil” so its a nil value

At 00:00 the script broke and this error had started to occur in this script.
This Script was Working in 2019.

2 Likes

You only return the two values when var2 is smaller than or equal to months[i]. If it’s not, all it does is subtract months[i] from var2 and assign var2 to that result. Perhaps you want to return something else in the else? (no pun intended)


By the way, don’t post images of exceptions as images can often be hard to read, and they often contain very large stack traces, which are hard to fit into an image. You can fix most exceptions by copying the message and pasting it into search, and finding similar problems. With an image, we are being forced to write it out, which can lead to potential transcription errors which can make the search less successful.

Why not just use the built-in function os.date() to get the date+time values?

-- https://developer.roblox.com/en-us/api-reference/lua-docs/os
local dict = os.date("*t", os.time())
print(("%04d-%02d-%02d %02d:%02d:%02d"):format(
	dict.year, 
	dict.month, 
	dict.day, 
	dict.hour, 
	dict.min, 
	dict.sec
))
2 Likes