Custom function returning exact OS date

OS.Date() weren’t correct so I made this hacky piece of code that grabs your local time, I will not be explaining how does this piece of codes works because its way too hacky even for you to understand. Once you run this code inside of a LocalScript it will return your OS’ date. This probably only works on Windows because of the fact that I haven’t tested this on MacOS or Linux clients yet, enjoy!

-- Property of @GamebringerDev


local t = tick()

local TypeOS = 0
local Sec = true
local Year = 1970
local Month = 1
local Day = 1
local Date = "January 1, 1970"
local Hour = 0
local Minute = 0
local Second = 0
local Tag = "AM"
local Time = "12:00 AM"

function getDate()
	local Months = {
		{
			"January",
			31,
			31
		},
		{
			"February",
			59,
			28
		},
		{
			"March",
			90,
			31
		},
		{
			"April",
			120,
			30
		},
		{
			"May",
			151,
			31
		},
		{
			"June",
			181,
			30
		},
		{
			"July",
			212,
			31
		},
		{
			"August",
			243,
			31
		},
		{
			"September",
			273,
			30
		},
		{
			"October",
			304,
			31
		},
		{
			"November",
			334,
			30
		},
		{
			"December",
			365,
			31
		}
	}
	
	Year = math.floor(1970 + t / 31579200)
	if Year % 4 == 0 then
		Months[2][3] = 29
		for i, v in pairs(Months) do
			if i ~= 1 then
				v[2] = v[2] + 1
			end
		end
	end
	Day = math.floor(t / 86400 % 365.25 + 1)
	for i, m in pairs(Months) do
		if m[2] - m[3] <= Day then
			Month = i
		end
	end
	local m = Months[Month]
	Day = Day + m[3] - m[2]
	Year, Day = tostring(Year), tostring(Day)
	local c, s = ", ", " "
	Date = Months[Month][1] .. s .. Day .. c .. Year + 1
end

function getTime()
	TypeOS = 1
	Second = math.floor(t % 60)
	Minute = math.floor(t / 60 % 60)
	Hour = math.floor(t / 3600 % 24)
	Second = tostring((Second < 10 and "0" or "") .. Second)
	Minute = tostring((Minute < 10 and "0" or "") .. Minute)
	Tag = TypeOS == 0 and (Hour < 12 and "AM" or "PM") or ""
	Hour = (TypeOS == 1 and Hour < 10 and "0" or "") .. tostring(TypeOS == 0 and (Hour == 0 and 12 or Hour > 12 and Hour - 12 or Hour) or Hour)
	local c, s = ":", TypeOS == 0 and " " or ""
	Time = Hour .. c .. Minute .. (Sec and c .. Second or "") .. s .. Tag

end

function StartFunction()
	while true do
		t = tick()
		getDate()
		getTime()
		print(Time..", "..Date)
		wait()
	end
end


StartFunction()

4 Likes

You’re just creating a solution to a problem that was never here in the first place, what’s wrong with the normal os.date()?

1 Like

it doesn’t gives the correct date, atleast it didn’t when I tried and therefore I tried creating a solution for it.

1 Like

os.date seems to give the correct date for me

image

2 Likes

Please show the output and script where this “issue” happened.

2 Likes

I am not available on PC right now, however this script is better than OS.Date() anyway, I made a comparison between each and my function were faster than OS.Date() with the matter of 3 seconds. I don’t have a proof for it but you can check it out yourself anyway.

1 Like

Why is the date 1970?? Just asking.

1 Like

Sorry to be that guy but can’t you just do:

local Month = os.date("%B")
local Year = os.date("%Y")
local Hour = os.date("%I")
local AM = os.date("%p")
print(Year .. Month .. Hour.. AM)
1 Like

You’re welcome for being that one guy and I know that you can use OS.Date() but there’s a few reasons for why I wrote this function:

  • fun
  • OS.Date() not working for me
  • my function is faster than OS.Date()
  • my plan was to make something that doesn’t return any value from any website at all

info: I think the reason why OS.Date() weren’t working for me because I lost connection at the time

I gotta correct some info here: Unix time - Wikipedia

(sorry for bumping but I had to do this)

January 1st, 1970 is the Unix epoch, which is when Unix time begin counting up from 0. Many Lua functions (and many operating systems, like Unix) handle time in terms of seconds since the Unix epoch.

1 Like

Not only unix, all the operating systems I know about uses unix epoch to calculate the time. (in my knowladge MacOS kernel ‘Darwin’ and Windows kernel uses unix as the base…) so the code in the thread works on all operating system I guess, correct me if im wrong.

OS.date works fine, must be a skill issue tbh.

2 Likes

omg roblox made linux support?!?!?!?!?!??!?!?!

i dont understand what problem you are fixing here-- os.date works fine for me and returns the actual time

it were a skill issue as you said, I think that it’s still cool. its faster than OS.Date() and it works offline. (which OS.Date() doesn’t in my knowladge correct me if im wrong)

1 Like

Windows uses the “New Technology” (aka NT) kernel? It’s not based on anything as far as I know

1 Like

and it works offline.

Since this is a Roblox-only script, this is a pretty useless point.

Secondly, this is far slower than os.date. Not sure how you benchmarked it, but your script is many times slower than using os.date with formatting options.

Benchmark results (1 million iterations):

Your script: 2.7083891999209
os.date: 0.30978530005086

Thanks for the report, I will try to improve the performance in my other scripts.

Or, you can just use DateTime.now():ToLocalTime() which is much simpler

2 Likes

It’s impossible to get under os.date runtime with the amount of code you have, I recommend you just stick to os.date

1 Like