So indulging in a sweet treat could help people remain adapted to the rhythms of night and day.

The nutrient is found in many foods including green leafy vegetables, nuts, seeds, fish, beans, whole grains, avocados, yogurt, bananas and dried fruit.

But few people are aware it’s vital to almost every function and tissue in the body - supporting a healthy immune system, preventing certain cancers and cutting risk of a heart attack.

Now it seems magnesium also helps control how cells keep their own form of time to cope with the natural environmental cycle of day and night.

It’s expected to be linked to whole body clocks which influence daily cycles - or circadian rhythms - of sleeping and waking, hormone release, body temperature and other important bodily functions in people.

Although supplements are available most experts say it’s preferable to get magnesium naturally from your diet.

The surprising discovery could aid the development of chronotherapy - treatment scheduled according to time of day - for patients and new crop varieties with increased yields or adjustable harvesting seasons.

Experiments in three major types of biological organisms - human cells, algae, and fungi - found in each case levels of magnesium in cells rise and fall in a daily cycle.

Molecular analysis showed this oscillation was critical to sustain the 24-hour clock in cells. It also had an enormous impact on metabolism - how fast cells can convert nutrients into energy.

It was already known that magnesium is essential to help living things convert food into fuel.

But the study, published in the journal Nature, showed it also controls when this biological function takes place - and how efficiently.

Dr Gerben van Ooijen, of Edinburgh University, said: “Internal clocks are fundamental to all living things.

“They influence many aspects of health and disease in our own bodies but equally in crop plants and micro-organisms.

“It’s now essential to find out how these fundamentally novel observations translate to whole tissue or organisms to make us better equipped to influence them in complex organisms for future medical and agricultural purposes.”

Co-author Dr John O’Neill, of Cambridge University, added: “Although the clinical relevance of magnesium in various tissues is beginning to garner more attention how magnesium regulates our body’s internal clock and metabolism has simply not been considered before.

“The new discovery could lead to a whole range of benefits spanning human health to agricultural productivity.”