Can I improve my function to get saved randomized dates?

I have this script that makes a random date and saves that date. But I think there could be a better way to organize than what I have written to do this.

function setBuyerInfo(buyer)
	local function date()
		local r=math.round
		local _r=math.random
		local dates={
			[1]={
				d=r(_r(1,31)).."/",
				m=r(_r(1,12)).."/",
				y=r(_r(2300,2399))
			},
			[2]={
				d=r(_r(1,31)).."/",
				m=r(_r(1,12)).."/",
				y=r(_r(2300,2399))
			}
		}
		for i,_date in dates do
			local y1=dates[1].y
			local y2=dates[2].y
			if y2<y1 then
				repeat
					task.wait()
					y2=r(_r(2300,2399))
				until y2>y1
				dates[2].y=y2
			end
		end
		local returningTable={}
		if table.find(mdyCountries,player.Country) then
			returningTable={
				[1]=dates[1].m..dates[1].d..dates[1].y,
				[2]=dates[2].m..dates[2].d..dates[2].y
			}
		else
			returningTable={
				[1]=dates[1].d..dates[1].m..dates[1].y,
				[2]=dates[2].d..dates[2].m..dates[2].y
			}
		end
		return returningTable
	end
	local returningDate=date()
	local dates={
		[1]="Date Of Purchase: "..returningDate[1],
		[2]="Expected Arrival: "..returningDate[2]
	}
	if buyer=="callisto" then
		buyerInfo.BuyerName.Text="Buyer Name: Callisto Corperation"
		buyerInfo.Region.Text="Region: Vega"
		buyerInfo._Name.Text="Name: "..Players:GetNameFromUserIdAsync(1259467892)
		if buyerDates["callisto"] then
			buyerInfo.PurchaseDate.Text=buyerDates["callisto"][1]
			buyerInfo.TimedArrival.Text=buyerDates["callisto"][2]
		else
			buyerInfo.PurchaseDate.Text=dates[1]
			buyerInfo.TimedArrival.Text=dates[2]
			buyerDates["callisto"]=dates
		end
	elseif buyer=="innovation" then
		buyerInfo.BuyerName.Text="Buyer Name: Innovation Incorporated"
		buyerInfo.Region.Text="Region: Caelum"
		buyerInfo._Name.Text="Name: "..Players:GetNameFromUserIdAsync(799258)
		if buyerDates["innovation"] then
			buyerInfo.PurchaseDate.Text=buyerDates["innovation"][1]
			buyerInfo.TimedArrival.Text=buyerDates["innovation"][2]
		else
			buyerInfo.PurchaseDate.Text=dates[1]
			buyerInfo.TimedArrival.Text=dates[2]
			buyerDates["innovation"]=dates
		end
	elseif buyer=="kingslayer" then
		buyerInfo.BuyerName.Text="Buyer Name: United States Intelligence Community"
		buyerInfo.Region.Text="Region: Northern America"
		buyerInfo._Name.Text="Name: Nomad"
		if buyerDates["kingslayer"] then
			buyerInfo.PurchaseDate.Text=buyerDates["kingslayer"][1]
			buyerInfo.TimedArrival.Text=buyerDates["kingslayer"][2]
		else
			buyerInfo.PurchaseDate.Text=dates[1]
			buyerInfo.TimedArrival.Text=dates[2]
			buyerDates["kingslayer"]=dates
		end
	elseif buyer=="nasa" then
		buyerInfo.BuyerName.Text="Buyer Name: National Aeronautics And Space Administration"
		buyerInfo.Region.Text="Region: Andromeda"
		buyerInfo._Name.Text="Name: Dwight Eisenhower"
		if buyerDates["nasa"] then
			buyerInfo.PurchaseDate.Text=buyerDates["nasa"][1]
			buyerInfo.TimedArrival.Text=buyerDates["nasa"][2]
		else
			buyerInfo.PurchaseDate.Text=dates[1]
			buyerInfo.TimedArrival.Text=dates[2]
			buyerDates["nasa"]=dates
		end
	elseif buyer=="ray" then
		buyerInfo.BuyerName.Text="Buyer Name: Rey Industries Incorporated"
		buyerInfo.Region.Text="Region: The Void"
		buyerInfo._Name.Text="Name: "..Players:GetNameFromUserIdAsync(553619003)
		if buyerDates["ray"] then
			buyerInfo.PurchaseDate.Text=buyerDates["ray"][1]
			buyerInfo.TimedArrival.Text=buyerDates["ray"][2]
		else
			buyerInfo.PurchaseDate.Text=dates[1]
			buyerInfo.TimedArrival.Text=dates[2]
			buyerDates["ray"]=dates
		end
	end
end

Any ideas?

2 Likes

Code here is a liiiiiittle messy and hard to understand as an outsider - variable names aren’t particularly descriptive, especially your shorthand function references

In terms of generating random dates, there’s a much easier way - UNIX time is the seconds since the 1st of January, 1970. It is provided in UTC format (+00:00) and can be used to point to just about any time after 1970

For example, as I write this right now, the current UNIX time is 1723408943 - you can find this by googling “UNIX time now”

To get the UNIX time in your code from either client or server, you can use os.time() - remember that it will be in UTC timezone for both client and server, not the clients timezone or the servers timezone

If you wanted to generate a future date from the current time of code execution that would be 1 day in the future, you could use:

local futureTime = os.time() + (60*60*24)
Or, if you want to get rid of the seconds per day math:
local futureTime = os.time() + 86400

To use random dates, you can always just do os.time() + math.random(minSecs, maxSecs). If you wanted to do specific hours, like having it occur on a random day but always at 15:00, you could use modulo and/or rounding/math functions to achieve this functionality

To turn a UNIX timestamp (seconds) into an actual readable date/time, look into the os.date functionality here (apparently os.date is now deprecated, use the DateTime API instead)

It’s worth noting that UNIX time is very widely accepted and is the typical go-to for universal dates and times or working with dates/times when you need functionality like calculating 24 hours from the current date, or in your case for getting a random date in the future

Hope this helps!

Nice, thank you so much man : D

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.