Long hours of summer sunshine may improve our moods and provide ample opportunity for sunbathing, but what exactly does soaking in those rays do to our skin?

“What happens when the UV light hits our skin is the energy penetrates our skin and hits our DNA,” dermatological surgeon Jerry Brewer of the Mayo Clinic said. “One of the most common things that happens after that is the formation of a pyrimidine dimer. That’s when two different building blocks of DNA that are next to each other form a bond connecting them more tightly than they should be.

"So when our body is replicating that DNA, it’s hard to figure out what that is. Instead of seeing two building blocks, it sometimes sees it as only one. And that causes a frame shift mutation.”

Most of the time, Brewer said, the body is good about detecting those frame shifts, and it even has a repair system that uses the mirror image DNA strand to fix the broken one. But every once in a while, the repair system fails.

Still, there’s a backup system in which cells commit suicide if it’s changed too much. But when a cell slips by those two systems, “it sometimes gets changed in a very dramatic way that can lead to a lot of things, including cancer.”

10 Reasons You Should Never Go to the Beach -- Ever

Ironically, sunlight can also synthesize vitamin D, which is not only essential for bone growth and absorbing calcium, but can help prevent DNA damage (which shows up as a tan or a sunburn) by reducing oxidative stress, Brewer said.

It can also help regulate the growth of cells, important to the prevention of cancer. When the light hits your skin, a gene known as POMC (pro-opiomelanocortin) gets activated, which leads to increased melanin and increased beta endorphin.

“That’s almost like a drug; it can give a person an internal high, and that’s why some people can become addicted to tanning,” Brewer said.

It’s no excuse to lay out your beach blanket, though: it only takes a few minutes to absorb plenty of vitamin D, depending on your complexion. Darker skin can tolerate more time, whereas a light-skinned person would get plenty of exposure even wearing long sleeves and pants.

PHOTOS: Trashy Beaches are Everywhere

And slathering on the sunscreen isn’t a cure-all: Epidemiological data has pointed at UV light as a source of melanoma. New experimental research published in the journal Nature confirms both that it does and that sunscreen does not provide complete protection. The experiments on mice also pinpointed the exact gene that UV light mutates: Trp53.

So where’s the balance?

Summer Bummer: Beaches Can Make You Sick

“The bottom line is, DNA damage is bad, vitamin D is good. It’s best to reduce DNA damage by reducing UV exposure, and keep vitamin D levels up through diet,” Brewer said.

UVB radiation (from sunlight) triggers an enzyme to start the process of increasing melanin, which is synthesized inside cells called melanocytes. Once made, the melanin leaves the cell and darkens the color of your skin.

“We’re not trying to ruin it for everybody; people do want to go out and be in the sun,” said Richard Marais, author of the Nature study and director of Cancer Research U.K.'s Paterson Institute for Cancer Research.

“But we need to start to understand that it’s a more complex message -- slapping sunscreen on doesn’t mean you’re good to go for the day. You need to combine it with other sun protection, such as loose-fitting clothes, sunglasses, and staying out of the sun when it’s at its hottest point.”

Also, individual skin varies widely, and sun protection may take different forms from person to person, Marais said.