Trying to get am/pm based on TimeOfDay

You can compare the ClockTime instead of the TimeOfDay

You’ll want to do this instead:

if lighting.ClockTime >= 12 then
3 Likes

You realize there’s an actual integer value in lighting right? No need to do this on the string itself.

image

Edit: For the nitpickers out there, yes I’m aware its a float :eyes:

1 Like
lighting:GetPropertyChangedSignal('TimeOfDay'):Connect(function()
	local indicator
	if lighting.ClockTime >= 12 then
		indicator = 'pm'
	else
		indicator = 'am'
	end
	timer.Text = string.sub(lighting.TimeOfDay, 1, 5) .. ' ' .. indicator
end)

This seemed to work, however it shows as a 24 hour time, instead of 12 hour time

2 Likes

This may be unrelated but I saw a post about time lines of gamepass that which somewhere located in datastore that you can input the real day and time

1 Like

There’s a Date Module for this, I use the one by BlueTaslem, a really smart guy if I do say so myself. If you want real life time.

1 Like

I’m not after real life time. As players would all have different timezones, so you’d only be able to play during the day time for the most part. I’m just using in game time and having it change over a period of time

I was just responding to @Wh7sk about there being a far easier way to do this for real life time. For you however, I’m aware you just wanted to do AM/PM.

1 Like

If @Kymaraaa solved your post, do you mind marking it as the solution?

I still need the time to be 12 hour time tho. Atm it goes like 14:00 pm, instead of 2:00 pm

Just subtract 12. Make sure you are storing the hour locally. Like in a new variable so you don’t end up setting the time to 2AM. Also only subtract 12 when the 24 hour clock is greater than 12 as in @Kymaraaa post.

I’m setting time like this tho:

timer.Text = string.sub(lighting.TimeOfDay, 1, 5) .. ' ' .. indicator

Using lighting.ClockTime just makes it like 12.0526526725621

Then yeah you’ll want to store the time, Clock Time in a variable… and if its above 12 then subtract 12.

Also use ClockTime instead of TimeOfDay when setting the text. 12.35 should be 12:35PM

Here’s some code that works:

Lighting:GetPropertyChangedSignal('ClockTime'):Connect(function()
	local Hours = tonumber(string.sub(Lighting.TimeOfDay, 1, 2)) -- Gets the hours, turns 05 --> 5
	local Minutes = string.sub(Lighting.TimeOfDay, 4, 5) -- Gets the minutes, keeps the extra 0
	
	local String = '%s:%s %s'
	
	if Lighting.ClockTime >= 13 and Lighting ~= 0 then -- It's between 1pm and 11:59pm
		String = String:format(Hours - 12, Minutes, 'pm')
	elseif Lighting.ClockTime >= 12 and Lighting.ClockTime < 13 then -- It's between 12pm and 12:59pm
		String = String:format(Hours, Minutes, 'pm')
	elseif Lighting.ClockTime >= 0 and Lighting.ClockTime < 1 then -- It's between 12:00am and 12:59am
		String = String:format(12, Minutes, 'am')
	else -- It's between 1:00am and 11:59 am
		String = String:format(Hours, Minutes, 'am')
	end
	
	timer.Text = String
end)
9 Likes

Hah, seems I was doing it the long way.

1 Like

Seems to work, only bug I saw was at midnight it’s 0:00 am instead of 12:00 am

As well, the time is showing as

1 :10 pm. How can I remove the spacee between the hour and the colon?


Also, 12:00 pm is 00:00 pm for some reason

Updated my post to address those issues you stated. The way you want midnight to display is strange, generally you’d want it to be 00:00, but it’s alright.

That’s because the 24 hour clock goes up to 23 and then trips back to 0 for 12AM. Why? no clue. I’m on the 12 hour clock :smiley:

Seems to work better now :smiley: small detail that I’m picky on :grimacing:
During pm, it’s 1:10 pm (which is how I want it)


However, during am, it’ adds a 0 before the numbers. How can I remove said 0 from the start of the am times?

Updated my post to address this, you just had to tonumber() the Hours and Minutes.

1 Like

I do it like this:

local hour12 = (math.floor(Lighting.ClockTime)+11)%12+1
local minute = math.floor(60*(Lighting.ClockTime%1))
local suffix = (Lighting.ClockTime>=12) and "PM" or "AM"

For display in a standard clock string, it would be something like:

local timeStr = tostring(hour12)..":"..(minute<10 and "0" or "")..tostring(minute).." "..suffix
8 Likes