• There's a popular theory that it takes 21 days to form a habit
  • Researchers looked at two habits: washing hands and going to the gym
  • It actually takes months to develop a gym-going habit

Those trying to form a nice new habit may be wondering how long it would take before it starts to take shape. Unfortunately, there's no "magic number" for it, not even the famed 21-day time.

You might have heard of the idea that it takes 21 days to form a new habit. The idea is simple: do the new activity (or perhaps quit the behavior) for 21 days and then it should be a new habit after that.

This idea likely stems from a 1960 book by plastic surgeon Dr. Maxwell Maltz, wherein he noted that it seemed to take patients 21 days to adapt to the change.

But is this really applicable to forming new habits?

A team of scientists used machine learning to study habit formation — reportedly the first ones to do so, according to the California Institute of Technology (Caltech). For their study, which was published Monday in the journal Proceedings of the National Academy of Sciences, the team focused on two habits: hand washing and going to the gym.

They partnered with 24 Hour Fitness to look at the data of people who swiped their badges to enter gyms over the course of four years. They also collected data from a company that uses radio frequency identification (RFID) to monitor hand washing among hospital workers during their shifts, covering 100 shifts.

The researchers found rather interesting results.

"Contrary to the popular belief in a 'magic number' of days to develop a habit, we find that it typically takes months to form the habit of going to the gym but weeks to develop the habit of handwashing in the hospital," the researchers wrote.

It takes an average of about six months to form the gym-going habit, which is quite far from the 21-day theory.

The idea that it takes 21 days to form a habit is "not based on any science," study author Colin Camerer, of Caltech, said in the release. The study shows the time taken to form a habit depends on the habit itself, as well as on various other factors.

For instance, about 69% of the gymgoers went to the gym on the same days of the week. The time that had passed since a previous gym visit also plays a role in understanding whether someone would visit again — the longer the gap since the last visit, the less likely it was to become a habit.

"There is no magic number for habit formation," another study author, Anastasia Buyalskaya of École des hautes études commerciales de Paris (HEC Paris), said in the Caltech release.

Forming a habit doesn't just happen simply because one did it for a set number of days.

The study also highlights the use of machine learning in looking at people's habits in the natural environment, the researchers said. In previous such studies, people simply had to answer surveys. Using machine learning, instead, allowed them to have an objective measurement of gym attendance and hand washing.

"With machine learning, we can observe hundreds of context variables that may be predictive of behavioral execution," Buyalskaya noted. "You don't necessarily have to start with a hypothesis about a specific variable, as the machine learning does the work for us to find the relevant ones."

Here, gym equipment is pictured January 9, 2017. Pixabay