How do I change a date into the number of seconds from now?

I am making a contest that will start on a certain date(let’s say March 13, 12:30) I want a to make a counter that counts down the seconds until it reaches that date. I have done some research on how to get dates and so on(lol), but I am still confused as to how to make this function.

This is what I made so far:

local function dateToSeconds(year,yday,hour,min,sec)
	local returnValue
	local localData = os.date("*t", os.time())
	if year == localData.year then
		if yday == localData.yday then
			if hour == localData.hour then
				if min == localData.min then
					returnValue = sec - localData.sec	
				else
					local minutes = (min - localData.min) * 60	
					returnValue = (min + sec) - localData.sec
				end	
			else
				local hours = ((hour - localData.hour) * 60)*60
				returnValue = (min + sec + hours) - (localData.min*60) - localData.sec
			end	
		else
			local ydays = (((yday - localData.yday) * 24)*60)*60
			returnValue = (min + sec + hour + ydays) - (localData.min*60) - ((localData.hour*60)*60) - localData.sec
		end
	else
		local years = ((((year - localData.year) *365)*24)*60)*60
		returnValue = (min + sec + hour + yday + years) - (localData.min*60) - ((localData.hour*60)*60) - (((localData.ydays* 24)*60)*60) - localData.sec
	end	
	return returnValue
end
1 Like

You can make your own custom date table in the format returned by os.date and use os.time on it to convert it to seconds:

local function secondsUntilDate(year, month, day, hour, minutes, seconds)
  local date = {year=year, month=month, day=day, hour=hour, min=minute, sec=seconds}
  local currentTime = os.date("*t")
  return os.time(date)-os.time(currentTime)
end

The issue is getting the date of something in local time, which is to be honest a pain. Time zones are awful. But assuming you can solve that problem (it might be as simple as os.time(os.date("*t", os.time(dateTable))), I haven’t tested that since I’m on mobile) then the function I provided should work.

1 Like