Noob question on os.date() (SOLVED)

Hi defolders,

I’m trying to track player’s streak in a game using

os.date( "%d" ) 

but something unusual happens or at least it is unusual to me haha.

if I do:
(let’s asume for this example today is June 18)

local today = os.date( "%d" ) -- 18
local streak = 0
if today == 18 then 
    streak = streak + 1
end
print(streak) -- 0

Defold doesn’t make the comparison. The only way I can get Defold to do the comparison in the expected way is this way:

local today =tonumber( os.date( "%d" )) -- 18
    local streak = 0
    if today == 18 then 
        streak = streak + 1
    end
    print(streak) -- 1

Can anyone tell me why is that? When I use

hour = os.date( "%H" )

I can perform comparisons with the hour without the need of tonumber().

I would really appreciate some light in the matter.

Cheers.

os.date("%d") returns a string in the format 01-31. Lua will do automatic string and number conversion when possible but being explicit about it using tonumber() and tostring() is always better. Let’s say that it is the 9th then os.date("%d") will return “09”. If you compare “09” == 9 then I’m pretty sure that will end up as “09” == “9” which obviously is not the same thing. But if you do tonumber(“09”) == 9 that will become 9 == 9.

1 Like

As always thanks a lot @britzl it makes a lot of sense.

2 Likes