Why does our skin/body produce vitamin D when tanning?

Is it a sort of defense mechanism against the UV or something?


UV contains vitamin D. Our body produces it when we're in the sun because it's being absorbed into your skin from UV rays.

EDIT: Tanning excessively is not healthy. the only healthy reasons for tanning is for the vitamin d and it can be good for your complexion.