For the earlier (Apollo 11-12, 13 in training) suits there were four visors:
- an inner protective visor which is 'an ultraviolet-stabilized polycarbonate shield which affords impact, micrometeoroid, and ultraviolet ray protection' (text from ALSJ, reference below);
- a gold-coloured sun visor which I think is to deal with visible light and IR – 'the inner surface of the polysulfone sun visor has a gold coating which provides protection against light and reduces heat gain within the helmet' (source ALSJ again);
- two side visors which are, I think, to stop sunlight from the side illuminating the astronaut's face and the inside of the helmet (they're basically what a photographer would call a lens hood or a lens shade).
The later helmets were more complex and had, I think things a bit like peaked caps to provide further shade.
In summary the helmets had multiple mechanisms for protecting the astronauts and the pressure-tight part of the suits both from physical damage and from the UV, visible and IR components of sunlight. These mechanisms changed somewhat over the programme.
This is well described in the Apollo Lunar Surface Journal (ALSJ above), which is an invaluable resource.
As a note, it's important to remember that the Sun is quite nasty above the atmosphere: the atmosphere absorbs about 77% of solar UV, and about 25% of all solar radiation. So if you are working on the Moon (or generally above the atmosphere) you really want protection from the Sun: even though the Moon has a fairly low albedo as a whole this is not something you want to rely on, and things like spacesuits and spacecraft are fairly reflective and often in your line of sight. You also need to worry about sunburn on your face as well as your eyes. Finally the Sun will dump quite a lot of heat (IR) into the suit unless you keep it out, which will certainly cause stress to the cooling system and also probably just be uncomfortably hot: think of how hot the Sun is on your skin on a completely cloudless day somewhere close to the equator: the flux hitting your face on the Moon is about 25% over that.