Os.date()

Hey there,

I would love to use os.date() on ROBLOX, so that we can create calendars and automatic systems that will work by date :slight_smile:
We can also do time stamps on date this way.

Example :

  • A ModuleScript that will make the entire game ready for Christmas
  • A Shop that will have daily dails
  • A Shop that will have ‘summer’ deals, when it is truely summer.
  • A Shop that will have ‘weekly’ deals

Juriaan

6 Likes

Before any of you start that this is already possible with Math :

  • Yes, it is. But it is a pain in the butt to have all those exceptions.
1 Like

os.date() would be a nice thing to have – it’s fairly annoying to have to convert seconds to date when all you want is mm:dd:yyyy e.e

I wonder why .date() was removed from os in the first place as well.

wait how do we use math to get the date?

1 Like
months = {"January","February","March","April","May","June","July","August","September","October","November","December"}
daysinmonth = {31,28,31,30,31,30,31,31,30,31,30,31}

function getDate()
	time = os.time()
	local year = math.floor(time/60/60/24/365.25+1970)
	local day = math.ceil(time/60/60/24%365.25)
	local month
	for i=1, #daysinmonth do
		if day > daysinmonth[i] then
			day = day - daysinmonth[i]
		else
			month = i
			break
		end
	end
	
	return month, day, year
end

local month, day, year = getDate()
root.CompletedTasks.CurrentDate.Text = "Today's Date is: "..month.."/"..day.."/"..year

Straight off of free models – readily available to all.

7 Likes

@Echo.
The math behind it isn’t really accurate. Leap years for example aren’t done in that code :wink:
And I really don’t understand why we have os.time() and os.difftime() and not os.date()

2 Likes

[quote] @Echo.
The math behind it isn’t really accurate. Leap years for example aren’t done in that code :wink: [/quote]

I wouldn’t expect any better – it was on free models. :wink:

I’m with you on that one.

Is there an easily-accessible API that you can read the month/day/year from somewhere out there (using HTTPService)? Anyone who could take that and make it into a script would be a hero!

Could just grab it off of this site

It won’t be accurate since time is taken for the HTTP request to reach both ends.

It won’t be accurate since time is taken for the HTTP request to reach both ends.[/quote]When you’re just trying to get it down to the DAY, I don’t think it matters that much.

Could just grab it off of this site[/quote]Any clue how? I’m a total noob to HTTPService, so…

Guys. I’m aware that this is possible using HTTPService, but could we please have it in ROBLOX without HTTPService…

1 Like

Ahahahahahaha XD

1 Like

I would rather have some TimeToDate function that takes a unix time argument and returns a tuple of the date values (seconds, minutes, hours, etc.). This way we can get the date of arbitrary times.

Ahahahahahaha XD[/quote]

That’s not a helpful post at all. Making fun of people for asking legitimate questions isn’t okay IMO.

2 Likes

It won’t be accurate since time is taken for the HTTP request to reach both ends.[/quote]When you’re just trying to get it down to the DAY, I don’t think it matters that much.

Could just grab it off of this site[/quote]Any clue how? I’m a total noob to HTTPService, so…[/quote]

Here, I made something simple. It takes two arguments: Timezone and Pattern.
Use this to construct patterns based on PHP’s date()

Example:
http://osyrisrblx.com/date.php?Timezone=EST&Pattern=Y-m-d%20H:i:s

So to get that in ROBLOX you’d do something in Lua like…

local DateString=game:GetService("HttpService"):GetAsync('http://osyrisrblx.com/date.php?Timezone=EST&Pattern=Y-m-d%20H:i:s')

Don’t use ‘date()’ - use ‘gmdate()’ (I think that’s the function) - ‘gmdate()’ returns what ‘os.time()’ would in Lua.

Good timing! My timestamp() function stopped working recently (badly coded, but lasted 2 years), so I started writing a new one. Little did I know it would take me 5 hours. Christ. I have no idea, I was much better when I was 15. If it’s ever taking you that long to do something, stop, take a break and rethink. I wasted too much time.

EDIT: Forgot to mention, IT SUPPORTS LEAP YEARRRSSSSS!!

But here it is: (sorry if it’s hard to read I have a blue theme to “keep me” cool, I hate the Summer heat.)

And the code, change the timezone to your liking. I live in EST.

function getMonth(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 = "EST", -4
	local secondspassed = os.time()+(3600*offset_hours)
	local yearspassed = math.floor(secondspassed/31557600)
	local YoTc = yearspassed-30 -- 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 = getMonth(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.." "..timezone..", Day of the Year: "..DoTy
end
10 Likes