How do I use os.date?

One last question, why do I make %x a string?

I didn’t really find my answer but thats okay. Thank you.

Here’s how I use it:

 local function zeropad(number,digits)

	if number==nil then
		print("WARNING -- zeropad called but number is nil")
		return
	end
	local str = tostring(number)
	local padding = "000000000000000000000000000000000000000000000000000000000000"
	if digits==nil then
		digits=10
	end
	local result = string.sub(padding,1,digits-string.len(str))..str
	return result
	
	
end    
 function gettime_date()

	     local tm_ar = os.date("!*t", os.time())

	     local today_date = tm_ar['year']..zeropad(tm_ar['month'],2)..zeropad(tm_ar['day'],2)..zeropad(tm_ar['hour'],2)..zeropad(tm_ar['min'],2)..zeropad(tm_ar['sec'],2)

	return today_date
	
end

I don’t understand the code sorry.

the gettime_date function will return a string telling you the current server date and time in a string format. Might be useful… giving the year, month, date, hour, minute and second.

what does the os.date("!*t", os.time()) mean and why does the variable padding have all those zeros?

doing os.date("!*t",os.time) returns to you a table with the “year” “month”, “day”,“hour”, “second” and “minute” keys, and their values.

The other function I included puts a zero in front of each number, as needed - its for formatting.

So for example the minute value might be 5… the zero padding function converts this to a string, to read as “05”

January will be a simple number 1… if you zero pad the value in the string you get “01”

So instead of “2021-8-2 7:8:0”,

the string will read “2021-08-02 07:08:00”
using the zero padding function.

Try it out.

1 Like

Thanks dude. I wish I could also mark your answers as solution but I have multiple people that helped me. Also, does it matter how many zeros you put?